简体   繁体   English

将jQuery的click事件绑定:获取浏览器事件,而不是jQuery事件

[英]Binding `click` event with jQuery: getting browser event, not jQuery event

I have this code: 我有以下代码:

$link.click(function (e) {
    debugger;
});

When the link is clicked and the debugger engages, e is a regular browser event, and not a jQuery event. 当单击链接并且调试器参与时, e是常规浏览器事件,而不是jQuery事件。 There's no .stopPropagation() or .preventDefault() . 没有.stopPropagation().preventDefault()

Why is this? 为什么是这样?

Try 尝试

$link.bind("click", function (e) {
    var jQueryEvent = e,
        browserEvent = e.originalEvent;
});

Because, as you said, it a regular browser event, and browser event doesn't have that kind of method. 正如您所说,这是常规的浏览器事件,而浏览器事件没有这种方法。

To stop the propagation, just return false . 要停止传播,只需return false

e should be an instance of jQuery.Event , so those methods should be available: e应该是jQuery.Event的实例,因此这些方法应该可用:

$("a").click(function(e) {
    alert(e instanceof jQuery.Event); // true
});

http://jsfiddle.net/dyJtY/ http://jsfiddle.net/dyJtY/

Can you provide more context, such as the debugger output? 您能否提供更多上下文,例如调试器输出?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM