-
1、添加事件
添加事件方式
var odiv = document.getElementById('div')
odiv.onclick = function () {}
-
顯示隱藏圖片
操作div的display屬性,在block和none之間切換即可 -
this使用
在匿名函數(shù)中的this就是當(dāng)前對(duì)象
在onclick=demo(this) 就是當(dāng)前節(jié)點(diǎn)
修改內(nèi)容
this.innerHTML = 'xxx' -
切換背景色
表單內(nèi)容控制
<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'
}
}
-
2、onload函數(shù)
window的事件,windows.onload = function () {} 是在整個(gè)文檔加載完畢之后執(zhí)行,但是自己寫的onclick的點(diǎn)擊函數(shù)不能寫到onload里面,因?yàn)閮?nèi)部函數(shù)只能在內(nèi)部使用,不能再外部使用
-
3、定時(shí)器
定時(shí)器:分為兩種,一種是周期性定時(shí)器,一種是一次性定時(shí)器
周期性:每隔5s執(zhí)行一次函數(shù)
一次性:幾秒之后執(zhí)行一次函數(shù),執(zhí)行完畢定時(shí)器結(jié)束
var timer = setTimeout(fn, 5000)
5000ms之后執(zhí)行fn一次。然后結(jié)束
銷毀定時(shí)器 clearTimeout(timer)
計(jì)數(shù)器
圖片消失 -
4、獲取非行內(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]
}
-
5、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è)頁面 -
6、DOM操作
children
parentNode
firstElementChild
lastElementChild
previousElementSibling
nextElementSiblingfirstChild
lastChild
previousSibling
nextSiblingtagName
createElement
removeChild
appendChild
insertBeforesetAttribute getAttribute
-
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ā)