What is the difference of using addEventListener?

此生再无相见时 提交于 2020-01-10 14:38:10

问题


What is the main difference between of using this...

document.addEventListener('mousedown', function() {
    // code
}, false);

...and this?

document.onmousedown = function() {
    // code
}


Will there be any different result or any cause?


回答1:


onclick is a property, like the onclick attribute can be placed in HTML. It has best browser support, however, it is primitive, as reassigning it overwrites the first (like any object property).

addEventListener(), as the name suggests, allows you to register multiple callbacks for an element and event type. This allows you to have multiple mousedown events for the same element. Before IE9, IE had their own attachEvent() which is similar (you must specify the on part too with attachEvent()).



来源:https://stackoverflow.com/questions/9028811/what-is-the-difference-of-using-addeventlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!