2月10
Day15
1-列举几个常见的浏览器兼容问题
1、滚动条
document.documentElement.scrollTop||document.body.scrollTop
2、获取样式兼容
function getStyle(dom, styleName){
return dom.currentStyle?
dom.currentStyle[styleName] :getComputedStyle(dom)[styleName];}
3、网页可视区兼容
window.innerHeight || document.documentElement.clientHeight
window.innerWidth || document.documentElement.clientWidth
4、事件对象兼容
e = e || window.event;
5、阻止事件冒泡兼容
event.stopPropagation? event.stopPropagation():event.cancelBubble=true;
6、事件目标对象兼容
var src = event.target || event.srcElement;
7、阻止默认行为兼容
evt.preventDefault?evt.preventDefault():evt.returnValue=false;
2-js 中给元素注册事件的方法有哪些?
Html元素:
<input type="button" id="btn1" value="插入节点到末尾" />
Js脚本
<script>
var btn1=document.getElementById('btn1');
//父节点
var div=document.getElementById('parent');
//给按钮添加单击事件
btn1.onclick=function(){
//1.创建节点
var h2= document.createElement('h2');
h2.innerHTML="我是新标题标签";
//2.往父节点中插入
div.appendChild(h2);
}
</script>
2<input type="button" name="btn2" value="删除一行" onclick="deleteRow(this)">
Js脚本
function deleteRow(btn){
//通过删除按钮找到它所在的行,再删除行
var tr= btn.parentNode.parentNode;
tr.parentNode.removeChild(tr);
}
3-js 中给元素的移除事件的方法
IE9以下不兼容
div.addEventListener('click',test,false)// 绑定事件
div.removeEventListener('click',test,false)// 移除事件
IE独有
div.attachEvent('onclick',test) // 绑定事件
div.detachEvent('onclick', test)// 移除事件
4-阻止冒泡的方法
e.preventDefault(); 阻止默认行为(表单)
e.stopPropagation() 阻止冒泡,不阻止默认行为(例如a超链接跳转)‘
return false 阻止冒泡,也阻止行为
5-阻止默认行为的方法
return false

浙公网安备 33010602011771号