jQuery相当于JavaScript的addEventListener方法 [英] jQuery equivalent of JavaScript's addEventListener method
问题描述
document.addEventListener('click', select_element,true);
我已经得到:
$(文件)。单击(select_element);
但是并没有达到相同的结果,像JavaScript方法的最后一个参数 - 一个布尔值这表示事件处理程序是否应该在捕获阶段或冒泡阶段执行(根据我从 http:/ /www.quirksmode.org/js/events_advanced.html ) - 没有了。
如何指定该参数,否则实现相同的功能,使用jQuery?
不是所有的浏览器都支持事件捕获(例如Internet Explorer版本少于9个)所有这些都支持事件冒泡,这就是为什么它是用于将处理程序绑定到所有跨浏览器抽象中的事件的阶段,包括jQuery。
最接近你的在jQuery中寻找使用 bind()
(由 on()
)或事件特定的jQuery方法(在这种情况下, click()
,其内部调用 bind()
)。所有这些都使用凸起事件的冒泡阶段。
I'm trying to find the jQuery equivalent of this JavaScript method call:
document.addEventListener('click', select_element, true);
I've gotten as far as:
$(document).click(select_element);
but that doesn't achieve the same result, as the last parameter of the JavaScript method - a boolean that indicates whether the event handler should be executed in the capturing or bubbling phase (per my understanding from http://www.quirksmode.org/js/events_advanced.html) - is left out.
How do I specify that parameter, or otherwise achieve the same functionality, using jQuery?
Not all browsers support event capturing (for example, Internet Explorer versions less than 9 don't) but all do support event bubbling, which is why it is the phase used to bind handlers to events in all cross-browser abstractions, jQuery's included.
The nearest to what you are looking for in jQuery is using bind()
(superseded by on()
in jQuery 1.7+) or the event-specific jQuery methods (in this case, click()
, which calls bind()
internally anyway). All use the bubbling phase of a raised event.
这篇关于jQuery相当于JavaScript的addEventListener方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!