事件对象:
type属性:返回string,表示当前事件的类型
wrap.ondblclick = function(e){
var e = e||window.event;
console.log(e,type);
//兼容写法
var el = e.target || e.srcElement;
}
target属性:
返回真正触发当前事件的元素。
存在兼容问题:IE9以下不支持,需要使用srcElement;
wrap.onclick = function(e){
console.log('wrap');
console.log('e.target');
}
在一些情况下我们不希望事件冒泡的发生,需要手动阻止
非IE下
通过事件对象的方法:
stopPorpagation( )
;
IE下
通过事件对象属性。cancelBubble
赋值为true;
var wrap = document.querySelector('#wrap'),
body = document.body;
wrap.onclick = function(){
console.log('wrap');
e.cancelBubble = true;
//ie兼容写法
if(e.stopPorpagation){
}
}
阻止默认事件的发生
document.querySelector('a').onclick = function(e){
var e = e || window.event;
//提示窗:confirm,用户可以做出选择,而用户选择的结果会作为confirm的返回值,确定就是true,取消就是false;
var res = confirm('确定要跳转到百度吗')
if(!res){
//阻止默认事件的发生;
//非IE下
//通过事件对象的方法:perventDefault():
e.preventDefault();
confirm('');
//IE下
//通过事件对象属性,returnVALUE赋值为false
if(e.preventDefault){
}
//通过return false :也可以阻止默认事件。但是要注意return 后的左右代码都不会在执行了:
}
}