day7_JavaScript(2)

2018-08-23  本文已影响0人  KingJX
var odiv = document.getElementById('div')
odiv.onclick = function () {}
<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'
    }  
}

window的事件,windows.onload = function () {} 是在整个文档加载完毕之后执行,但是自己写的onclick的点击函数不能写到onload里面,因为内部函数只能在内部使用,不能再外部使用

IE浏览器获取非行内样式方式

    obj.currentStyle['name']

火狐和谷歌获取方式

    getComputedStyle(odiv, null)['width']

获取非行内样式的兼容性写法

    function getStyle(obj, name) {
        return obj.currentStyle ? obj.currentStyle[name] : getComputedStyle(obj, null)[name]
    }
上一篇 下一篇

猜你喜欢

热点阅读