简体   繁体   English

使用addEventListener有什么区别?

[英]What is the difference of using addEventListener?

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? 会有任何不同的结果或任何原因吗?

onclick is a property, like the onclick attribute can be placed in HTML. onclick是一个属性,就像onclick属性可以放在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. 顾名思义, addEventListener()允许您为元素和事件类型注册多个回调。 This allows you to have multiple mousedown events for the same element. 这允许您为同一元素提供多个mousedown事件。 Before IE9, IE had their own attachEvent() which is similar (you must specify the on part too with attachEvent() ). 在IE9之前,IE有自己的attachEvent()类似(你必须使用attachEvent()指定on部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这两种在 addEventListener 中使用回调的方法有什么区别? - What's the difference between these 2 ways of using a callback in addEventListener? `onmessage` 和 `.addEventListener` 有什么区别? - What the difference between `onmessage` and `.addEventListener`? window.onscroll 和 window.addEventListener 有什么区别 - What is the difference between window.onscroll and window.addEventListener 使用 addEventListener - Using addEventListener 可以使用addEventListener向WinJS.Promise添加哪些事件? - What events can be added using addEventListener to WinJS.Promise? addEventListener() 中的布尔值“usecapture”参数和替代选项对象的“capture”属性有什么区别? - What is the difference between the boolean “usecapture” argument in addEventListener() and the “capture” property of the alternative options object? `HTMLImports.whenReady`和`window.addEventListener('WebComponentsReady',function(e){`之间有什么区别? - What is the difference between `HTMLImports.whenReady` and `window.addEventListener('WebComponentsReady', function(e) {` addEventListener 与 currentTarget 的区别 - Difference between addEventListener vs currentTarget JavaScript:什么是 addEventListener? - JavaScript: What is addEventListener? 在JavaScript中使用=和:有什么区别 - what is difference using = and : in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM