JS的事件和JS的阻止事件

1、事件的绑定方式
(1)将事件和响应行为都内嵌到html标签中

(2)将事件内嵌到html中而响应行为用函数进行封装


(3)将事件和响应行为 与html标签完全分离



****this关键字
this经过事件的函数进行传递的是html标签对象



2、阻止事件的默认行为
IE:window.event.returnValue = false;
W3c: 传递过来的事件对象.preventDefault();
//ie:window.event.returnValue = false;
//W3c:传递过来的事件对象.preventDefault();
//W3c标准
if(e&&e.preventDefault){
alert("w3c");
e.preventDefault();
//IE标签
}else{
alert("ie");
window.event.returnValue = false;
}


//通过事件返回false也可以阻止事件的默认行为
点击我吧

3、阻止事件的传播
IE:window.event.cancelBubble = true;
W3c: 传递过来的事件对象.stopPropagation();
if(e&&e.stopPropagation){
alert("w3c");
e.stopPropagation();
//IE标签
}else{
alert("ie");
window.event.cancelBubble = true;
}

你可能感兴趣的:(JS的事件和JS的阻止事件)