day15

1-列举几个常见的浏览器兼容问题

1、滚动条

document.documentElement.scrollTop||document.body.scrollTop

 

2、获取样式兼容

 

function getStyle(dom, styleName){ 

 

 

return dom.currentStyle?

dom.currentStyle[styleName] :getComputedStyle(dom)[styleName];}

4、事件对象兼容

e = e || window.event;

5、阻止事件冒泡兼容

event.stopPropagation? event.stopPropagation():event.cancelBubble=true;

7、阻止默认行为兼容

evt.preventDefault?evt.preventDefault():evt.returnValue=false;

2-js 中给元素注册事件的方法有哪些?

<body>
<div id="good" style="height:100px; width:100px;background:#323923"></div>
<script>
document.getElementById("good").attachEvent("onclick", function() {alert("haha");});
</script>
</body>

 

3-js 中给元素的移除事件的方法

<script>

var div = document.getElementsByTagName("div")[0]

function test(){

console.log(1)

}

// 兼容性很好

div.onclick = test // 绑定事件

div.onclick = ''; // 移除事件

// IE9以下不兼容

div.addEventListener('click',test,false)// 绑定事件

div.removeEventListener('click',test,false)// 移除事件

// IE独有

div.attachEvent('onclick',test) // 绑定事件

div.detachEvent('onclick', test)// 移除事件

</script>

 

4-阻止冒泡的方法

event.stopPropagation( )

event.target

现在,事件处理程序中的变量event保存着事件对象。而event.target属性保存着发生事件的目标元素。这个属性是DOM API中规定的,但是没有被所有浏览器实现 。jQuery对这个事件对象进行了必要的扩展,从而在任何浏览器中都能够使用这个属性。通过.target,可以确定DOM中首先接收到事件的元素(即实际被单击的元素)。

 

5-阻止默认行为的方法

1.a标签链接跳转

<a href="https://www.baidu.com/" target="_blank" id="skip">百度</a>

2.form表单提交

posted @ 2021-02-25 15:57  雨辰~  阅读(59)  评论(0)    收藏  举报