admin管理员组文章数量:1427301
I was wondering how to achieve jQuery's .live functionality with "traditional" JavaScript. I want something like $('a').live('mouseover', mouseover_func) to be written as usual JavaScript. But how?
I was wondering how to achieve jQuery's .live functionality with "traditional" JavaScript. I want something like $('a').live('mouseover', mouseover_func) to be written as usual JavaScript. But how?
Share Improve this question asked Feb 13, 2011 at 16:19 Rainer ZufallRainer Zufall 611 bronze badge2 Answers
Reset to default 10Bind a "mouseover" event handler to the <body>
element. In that handler, check the "target" property of each event it catches and see whether its "tagName" property is "A". If so, call the handler.
The "live" feature leverages event "bubbling", which is the name for the browsers process of checking for handlers from the target element to the root of the DOM, one parent at a time. Since each <a>
in your document can ultimately be traced back up to the <body>
, that root node will get all the "mouseover" events that aren't shunted off by lower-level handlers that cancel bubbling (via the "stopPropagation()" method on the event object, or some weird browser-specific way I guess).
Not all events bubble, however. I'm looking around for a good reference ... ok here, the MDC page seems pretty good though a little old maybe.
Just read the jQuery source code to see how it does it. That's one of the joys of open source. You will learn an awful lot...
本文标签: javascriptjQuery39s live functionality without jQueryStack Overflow
版权声明:本文标题:javascript - jQuery's live functionality without jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745493103a2660690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论