开发者

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. T开发者_StackOverflow中文版here's no .stopPropagation() or .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.


e should be an instance of jQuery.Event, so those methods should be available:

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

http://jsfiddle.net/dyJtY/

Can you provide more context, such as the debugger output?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜