事件监听器与 html 事件相同吗?
HTML 中的 onclick
与 jQuery 中的 .click()
相同吗?
显然,从可用性的角度来看它们是相同的,但我很好奇浏览器如何处理它们。
Is onclick
in HTML identical to .click()
in jQuery?
Obviously they are identical from a usability standpoint, but I am curious how the browser handles them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery 在幕后使用 JavaScript 附加事件。
一个重要的区别是 jQuery 允许使用
click()
绑定多个事件,而使用onclick
只能附加一个处理程序。这是因为 jQuery 使用addEventListener< /code> 或
attachEvent
在幕后,而不是绑定到直接.onclick
。此外,通过 JavaScript(使用或不使用 jQuery)附加处理程序可以促进不引人注目的 JavaScript(即不混合 JavaScript 和HTML),这是一件好事。
jQuery attaches events using JavaScript behind the scenes.
An important difference is that jQuery allows multiple events to be bound using
click()
, where as you can only attach one handler usingonclick
. This is because jQuery uses eitheraddEventListener
orattachEvent
behind the scenes, as opposed to binding to.onclick
directly.Furthermore, attaching handlers via JavaScript (using jQuery or not) promotes unobtrusive JavaScript (i.e. not mixing JavaScript and HTML), which is a good thing.
不,这不一样。
OnClick
设置 DOM 元素的属性,其中.click()
添加事件监听器。它们之间的区别在于每个 DOM 元素只能同时拥有一个类型的属性。因此,如果您在一个元素上使用两次
onClick=
,则只有最后添加的元素会获胜,第一个元素将被覆盖。这将始终发出警报 2,因为第一个附件将被覆盖:
使用
.click()
或.addEventListener('click', myFunction)
,您可以添加任意数量的函数如你所愿。因此,以下将警告 1 和 2:jquery
.click()
和.addEventListener()
之间的区别是,jquery 解决方案适用于所有浏览器,导致 IE.click()
=8 具有不同的语法 (attchEvent
)。并且您可以一次取消绑定所有点击处理程序。 普通 JavaScript 解决方案只能分离传递的函数,而不是全部。No it's not the same.
OnClick
sets a property of a DOM element, where.click()
adds an eventListener.The differnce between them is that every DOM element can only have on property of a type at once. So if you use
onClick=
twice on an element, only last added will win, the first will be overwritten.This will always alert 2, cause the first attachment will be overwritten:
Using
.click()
or.addEventListener('click', myFunction)
, you can add as many functions as you want. So the following will alert 1 and 2:The differnt between jquerys
.click()
and.addEventListener()
is, that the jquery solution works in all browser, cause IE<=8 has a different syntax (attchEvent
). And that you can unbind all click handlers in once. The normal JavaScript solution can only detach the passed function not all of them.(请注意,jQuery 是一个 JavaScript 库,因此如果您有时间的话,您无法做任何您自己在 JavaScript 中做不到的事情...)
jQuery
.click()
方法不同于onclick
在几个关键方面。排名不分先后:event
对象,这样您就不必担心浏览器之间的(大部分)细微差异.addEventListener() 或
.attachEvent()
取决于您的浏览器支持的内容,因此,您不必担心.onclick
无论如何,您只能绑定一个处理程序,但使用.addEventListener()
和.attachEvent()
则可以绑定多个处理程序).on()
或.delegate()
(或已弃用的.live()
), 附加事件,而不是快捷方法像.click()
一样,很容易设置事件委托在幕后,所有标准浏览器事件仍在发生,但 jQuery 为它们提供了一个包装器来实现上述所有操作。当然还有一些其他的区别,但我认为以上是最重要的。
。不,它们不是。更准确地说,jQuery 的事件在使用方式上与
.addEventListener()
或.attachEvent()
相同,但即便如此正如上面详述的,jQuery 为您提供了额外的抽象级别,使您不必自己编写所有代码。(Noting that jQuery is a JavaScript library and so can't do anything that you couldn't do in JavaScript yourself if you had the time...)
The jQuery
.click()
method is different toonclick
in a few key ways. In no particular order:event
object so that you don't have to worry about the (mostly) minor differences between browsers.addEventListener()
or.attachEvent()
depending on what your browser supports, so, again, you don't have to worry about the difference.onclick
you can only bind one handler anyway, but with.addEventListener()
and.attachEvent()
you can bind multiple handlers).on()
or.delegate()
(or the deprecated.live()
) to attach events, rather than shortcut methods like.click()
, it is easy to setup event delegationBehind the scenes all the standard browser events are still happening, but jQuery provides a wrapper for them to make all of the above happen. Of course there are some other differences, but I see the above as the most important.
No they're not. It would be much more accurate to say that jQuery's events are (almost) the same as
.addEventListener()
or.attachEvent()
in how you use them, but even then as detailed above jQuery gives you an extra level of abstraction to save you having to code it all yourself.即使在 JQuery 中,
.click()
也不一样。它是html中onclick
之上的一段代码。 JQuery 允许使用普通 html 事件之上的这一层将方法绑定到事件。您可以更改/覆盖
.click()
以满足您的需求。例如,当使用移动浏览器或 PDA 等时。the
.click()
even in JQuery is not the same. It is a piece of codes on top of theonclick
in html. JQuery allows to bind methods to a event using this layer on top of the normal html events.You can change/override
.click()
to adapt your needs. For instance when using a mobile browser or pda etc.