1、添加事件
添加事件方式
獲取模塊對(duì)象div
關(guān)聯(lián)點(diǎn)擊事件fun
實(shí)現(xiàn)事件功能
<body>
<div style="width:200px; height:200px; background-color:red" onclick="alert('這是一個(gè)div')"></div>
<div style="width:200px; height:200px; background-color:blue" onclick="test()"></div>
<div id="div" style="width:200px; height:200px; background-color:green"></div>
<script>
function test() {
console.log('花田里犯了錯(cuò),說好,破曉前忘掉')
}
var odiv = document.getElementById('div')
// odiv.onclick = function () {
// console.log('遙遠(yuǎn)的東方有一條龍')
// }
odiv.onclick = test
</script>
</body>
顯示隱藏圖片:
操作div的display屬性,在block和none之間切換即可
<body>
<div class="tu" style="display: block;">
<img alt="可愛" src="feng.jpg" "="">
</div>
<button id="btn">消失</button>
<button id="btn1" onclick="show()">出現(xiàn)</button>
<script>
// 顯示和隱藏圖片操作的是div的display屬性,在block和none之間切換
var obutton = document.getElementById('btn')
obutton.onclick = function () {
// 找到指定div,將其display屬性修改為none
var odiv = document.getElementsByClassName('tu')[0]
odiv.style.display = 'none'
}
function show() {
var odiv = document.getElementsByClassName('tu')[0]
odiv.style.display = 'block'
}
</script></body>
this使用
在匿名函數(shù)中的this就是當(dāng)前對(duì)象
在onclick=demo(this) 就是當(dāng)前節(jié)點(diǎn)
修改內(nèi)容
this.innerHTML = 'xxx'
<body>
<div class="tang" style="width: 400px; height: 200px; background-color: red;">秦時(shí)明月漢時(shí)關(guān),萬里長征人未還,但使龍城飛將在,不教胡馬度陰山</div>
<div class="tang" style="width: 200px; height: 400px; background-color: blue;" onclick="demo(this)"></div>
<script>
// 點(diǎn)擊div,將寬度有200px修改為400px
var odiv = document.getElementsByClassName('tang')[0]
odiv.onclick = function () {
console.log(this)
// this就是odiv
this.style.width = '400px'
// 給div添加內(nèi)容
this.innerHTML = '秦時(shí)明月漢時(shí)關(guān),萬里長征人未還,但使龍城飛將在,不教胡馬度陰山'
}
function demo(obj) {
// 這里面的this是window,在js里面寫的所有的函數(shù)都是window的函數(shù),調(diào)用demo其實(shí)就是 window.demo()
console.log(this)
// var odiv = document.getElementsByClassName('tang')[1]
obj.style.height = '400px'
}
</script></body>
切換背景色
<body>
<div id="bai" style="width: 300px; height: 300px; background-color: red;"></div>
<script>
var odiv = document.getElementById('bai')
odiv.onclick = function () {
// 先獲取div的背景色
color = this.style.backgroundColor
if (color == 'red') {
this.style.backgroundColor = 'yellow'
} else {
this.style.backgroundColor = 'red'
}
}
</script></body>
表單內(nèi)容控制
<script>
// clear是關(guān)鍵字,不能使用函數(shù)名
function cleara(obj) {
// console.log('clear')
if (obj.value == "請(qǐng)輸入用戶名") {
obj.value = ''
}
}
function recover(obj) {
if (obj.value == '') {
obj.value = '請(qǐng)輸入用戶名'
}
}
var oinput = document.getElementById('ip')
oinput.onfocus = function () {
// 判斷要不要清空
if (this.value == "請(qǐng)輸入用戶名") {
this.value = ''
}
}
oinput.onblur = function () {
// 判斷內(nèi)容是不是為空,如果為空變成下面這個(gè)
if (this.value == '') {
this.value = '請(qǐng)輸入用戶名'
}
}
</script>
2、onload函數(shù)
window的事件,windows.onload = function () {} 是在整個(gè)文檔加載完畢之后執(zhí)行,但是自己寫的onclick的點(diǎn)擊函數(shù)不能寫到onload里面,因?yàn)閮?nèi)部函數(shù)只能在內(nèi)部使用,不能再外部使用
如果實(shí)在是想用window.lala = function () {}
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>onload函數(shù)</title>
<script>
// var a = '岳云鵬'
// demo()
// onload是window的事件,意思就是等文檔加載完畢之后來執(zhí)行這個(gè)代碼
window.onload = function () {
// var odiv = document.getElementById('kong')
// console.log(odiv)
// odiv.onclick = function () {
// this.style.backgroundColor = 'cyan'
// }
}
function demo(obj) {
obj.style.backgroundColor = 'cyan'
}
</script>
</head>
<body>
<div id="kong" style="width: 300px; height: 300px; background-color: cyan;" onclick="demo(this)"></div>
<script>
// console.log(a)
// function demo() {
// console.log('你喜歡岳云鵬的相聲嗎?')
// }
</script>
<script>
// alert(a)
// demo()
function test() {
// function lala() {
// console.log('這是一個(gè)內(nèi)部函數(shù)')
// }
// lala()
window.lala = function () {
console.log('這是lala函數(shù)')
}
}
test()
// test.lala()
lala()
</script></body>
3、選項(xiàng)卡
手動(dòng)添加下標(biāo)
<style>
button {
width: 200px;
height: 100px;
font-size: 40px;
background-color: pink;
margin-left: 50px;
display: inline-block;
}
div {
width: 970px;
height: 600px;
font-size: 50px;
background-color: yellow;
margin-left: 50px;
margin-top: 50px;
display: none;
}
.active {
font-size: 50px;
background-color: blue;
}
.show {
display: block;
}
</style>
<body>
<button onclick="dudu(this, 0)">周杰倫</button>
<button onclick="dudu(this, 1)">王力宏</button>
<button onclick="dudu(this, 2)">張學(xué)友</button>
<button class="active" onclick="dudu(this, 3)">劉德華</button>
<div>稻香、菊花臺(tái)、發(fā)如雪、青花瓷、七里香、霍元甲、雙節(jié)棍、簡(jiǎn)單愛、千里之外、聽媽媽的話</div>
<div>花田錯(cuò)、龍的傳人、唯一</div>
<div>慢慢、吻別、一千個(gè)傷心的理由</div>
<div class="show">謝謝你的愛、冰雨、天意、忘情水</div>
<script>
// 得到所有的button
var abuttons = document.getElementsByTagName('button')
// 得到所有的div
var adivs = document.getElementsByTagName('div')
function dudu(obj, index) {
// 先將所有的button的class屬性設(shè)置為空
for (var i = 0; i < abuttons.length; i++) {
abuttons[i].className = ''
adivs[i].className = ''
}
// 給指定的button添加樣式
obj.className = 'active'
// 給指定的div添加樣式
adivs[index].className = 'show'
}
</script></body>
自動(dòng)添加下標(biāo)
<style>
button {
width: 200px;
height: 100px;
font-size: 40px;
background-color: pink;
margin-left: 50px;
display: inline-block;
}
div {
width: 970px;
height: 600px;
font-size: 50px;
background-color: yellow;
margin-left: 50px;
margin-top: 50px;
display: none;
}
.active {
font-size: 50px;
background-color: blue;
}
.show {
display: block;
}
</style>
<body>
<button class="active">李白</button>
<button>杜甫</button>
<button>李商隱</button>
<button>杜牧</button>
<div class="show">將敬酒,蜀道難,菩薩蠻,月下獨(dú)酌,夢(mèng)游天姥吟留別,宣州謝朓樓餞別校書叔云</div>
<div>石壕吏,潼關(guān)吏,新安吏,新婚別,無家別,垂老別</div>
<div>馬嵬,瑤池,無題,賈師,隨師東</div>
<div>清明,赤壁,秋夕,泊秦淮</div>
<script>
// 得到所有的button
var abuttons = document.getElementsByTagName('button')
var adivs = document.getElementsByTagName('div')
// 循環(huán)button數(shù)組,給里面每一個(gè)button添加點(diǎn)擊事件
for (var i = 0; i < abuttons.length; i++) {
// 給指定的button手動(dòng)添加一個(gè)屬性,用來保存是第幾個(gè)button
abuttons[i].index = i
abuttons[i].onclick = function () {
// 首先清掉所有button和div上面的class
for (var j = 0; j < abuttons.length; j++) {
abuttons[j].className = ''
adivs[j].className = ''
}
// 給指定的button添加樣式
this.className = 'active'
// console.log(i)
// 給指定的div添加樣式
adivs[this.index].className = 'show'
}
}
</script></body>
4、定時(shí)器
定時(shí)器:分為兩種,一種是周期性定時(shí)器,一種是一次性定時(shí)器
周期性:每隔5s執(zhí)行一次函數(shù)
一次性:幾秒之后執(zhí)行一次函數(shù),執(zhí)行完畢定時(shí)器結(jié)束
var timer = setTimeout(fn, 5000)5000ms之后執(zhí)行fn一次。然后結(jié)束
<body>
<div id="baby">
<img height="800" alt="氣質(zhì)美女" src="meinv.jpg">
</div>
<button id="btn">計(jì)時(shí)開始</button>
<script>
var odiv = document.getElementById('baby')
var obtn = document.getElementById('btn')
// timer就是一個(gè)定時(shí)器
var timer = setTimeout(function () {
odiv.style.display = 'none'
}, 5000)
obtn.onclick = function () {
// 清除timer這個(gè)定時(shí)器
clearTimeout(timer)
}
</script></body>
銷毀定時(shí)器 clearTimeout(timer)
<body>
<button onclick="demo()">一行白鷺上青天</button>
<script>
var timer = setInterval(function () {
console.log('兩個(gè)黃鸝鳴翠柳')
}, 5000)
function demo() {
clearInterval(timer)
}
</script></body>
小應(yīng)用:背景閃爍
<body>
<div id="lala" style="width: 300px; height: 300px; background-color: yellow;"></div>
<script>
var odiv = document.getElementById('lala')
setInterval(function () {
color = odiv.style.backgroundColor
if (color == 'red') {
odiv.style.backgroundColor = 'yellow'
} else {
odiv.style.backgroundColor = 'red'
}
}, 50)
</script></body>

計(jì)數(shù)器
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>計(jì)數(shù)器</title>
<style>
div {
width: 100%;
height: 300px;
line-height: 300px;
text-align: center;
font-size: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div id="dudu">2</div>
<script>
var odiv = document.getElementById('dudu')
// 定義一個(gè)全局變量,用來保存定時(shí)器對(duì)象
var timer = null
// 定義一個(gè)全局的計(jì)數(shù)器
var i = 0
odiv.onmouseover = function () {
timer = setInterval(function () {
i++
// 設(shè)置div的內(nèi)容
odiv.innerHTML = i
}, 1000)
}
odiv.onmouseout = function () {
clearInterval(timer)
}
</script></body>
5、獲取非行內(nèi)樣式
IE瀏覽器獲取非行內(nèi)樣式方式
obj.currentStyle['name']
火狐和谷歌獲取方式
getComputedStyle(odiv, null)['width']
獲取非行內(nèi)樣式的兼容性寫法
function getStyle(obj, name) {
return obj.currentStyle ? obj.currentStyle[name] : getComputedStyle(obj, null)[name]
}
6、BOM操作
window.setTimeout,window.setInterval
window.alert\window.confirm
window.open
window.history(back、go)
history.go(1) 去往前一個(gè)網(wǎng)址
history.go(2) 去往后一個(gè)網(wǎng)址
history.back() 倒退一個(gè)網(wǎng)址
location
href : 讀取得到當(dāng)前的url,設(shè)置跳轉(zhuǎn)到指定的url
reload() : 刷新整個(gè)頁面
7、select下拉框和oninput事件
onchange : 事件,用戶點(diǎn)擊下拉框觸發(fā)
selectedIndex : 用戶點(diǎn)擊的option的下標(biāo),下標(biāo)從0開始
options : osel.options 可以得到所有的option對(duì)象,這是一個(gè)數(shù)組
input框的oninput事件,只要內(nèi)容改變,就會(huì)觸發(fā)
<body>
<select name="刺客" id="sel">
<option value="1">阿珂</option>
<option value="2">蘭陵王</option>
<option value="3">孫悟空</option>
<option value="4">趙云</option>
<option value="5">李白</option>
</select>
<input id="ip" type="text">
<script>
var osel = document.getElementById('sel')
osel.onchange = function () {
// alert('觸發(fā)')
// alert(osel.selectedIndex)
alert(osel.options[osel.selectedIndex].innerHTML)
}
var oinput = document.getElementById('ip')
oinput.oninput = function () {
console.log(this.value)
}
</script></body>