Dom事件

Dom0级事件

var btn = document.getElementById('btn');

btn.onclick = function(event){

  alert(this.id);

};

所有浏览器都支持,作用域为当前绑定元素。IE下没有传入event对象,需要通过window.event获得;其它浏览器均传入event对象

Dom2级事件

var btn = document.getElementById('btn');

btn.addEventListener('click', function(event){

  alert(this.id);

}, false);

IE:

btn.attacheEvent('onclick', function(event){

  alert(this.id);

});

IE下绑定事件使用attacheEvent,其它浏览器及IE9+使用addEventListener;IE下作用域为window,其它浏览器为当前元素;所有浏览器都传入event对象。

posted @ 2012-05-22 21:26  shinebob  阅读(139)  评论(0编辑  收藏  举报