开发者

Use jQuery to find an event object

jQuery has wonderful selectors for DOM elements, but can you 'select' an event object.

That is, if I use inline JavaScript on the onclick attribute of an element, could I have a selector to find that event object so I can act on it.

I know I could do something like this:

$("a#some_link").click(function(event){//act on event here});

But how w开发者_如何学Goould I find the event object if inline JavaScript was used:

<a href="somepage.html" onclick="alert('how do I get my event oject?');">click me</a>

This question is a supplement to an earlier question: calling e.stopImmediatePropagation() from onclick attribute


onclick = "alert(event.type);return false;"

<a id="some_link" href="somepage.html" onclick="alert(event.type);return false;">click me</a>

Test it


Simple $('a').attr('onclick') gets you that data. In Firefox it's wrapped with function() { }


Well this should find 'onclick' events, I do not believe it picks up the jQuery bound events.

$("a,input").filter(function() {
    return $.isFunction(this.onclick)
});

If there are any other tag types you might have such an event on, then you would need to add them, you can use "*" as the selector, but that is quite slow.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜