开发者

Binding `click` event with jQuery: getting browser event, not jQuery event

开发者 https://www.devze.com 2023-02-22 00:26 出处:网络
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\'

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

精彩评论

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

关注公众号