admin管理员组文章数量:1289391
So, basically I have a plugin that I want called on an element if it is created in the dom,
E.g. let's say I have a plugin called domWatch that runs the function if the specified selector gets created inside the selector it was called on.
so:
$('#container').domWatch('.mySelector', function(element){
$(element).myPlugin();
});
$('#container').append($('<div />', {class: 'mySelector'})); //the new element should now have the myPlugin plugin called on it.
Is this possible to do?
So, basically I have a plugin that I want called on an element if it is created in the dom,
E.g. let's say I have a plugin called domWatch that runs the function if the specified selector gets created inside the selector it was called on.
so:
$('#container').domWatch('.mySelector', function(element){
$(element).myPlugin();
});
$('#container').append($('<div />', {class: 'mySelector'})); //the new element should now have the myPlugin plugin called on it.
Is this possible to do?
Share Improve this question asked Nov 3, 2012 at 12:31 HailwoodHailwood 92.7k112 gold badges273 silver badges425 bronze badges 2- LiveQuery is a way to do this, but if you are able to fire an event manually every time you append something to the dom, it might have better performance. – vinczemarton Commented Nov 3, 2012 at 12:45
- You can check something called Mutation events or better Mutation Observers. However, it isn't implemented in all major browsers yet. – dreame4 Commented Nov 3, 2012 at 12:47
5 Answers
Reset to default 6You can use the arrive.js library that I developed for the exact same purpose (uses Mutation Observers internally). Usage:
$('#container').arrive('.mySelector', function(){
$(this).myPlugin();
});
Have a look at this clever hack: http://www.backalleycoder./2012/04/25/i-want-a-damnodeinserted/. It uses CSS3 animations to detect when an element was inserted into the DOM.
The problem with 'listening for when a DOM element is created' is the performance hit, so maybe you should consider a different approach to solve your problem.
I'm sure there are many, way simpler ways to achieve your goal.
If you are targeting very modern browsers only you could check out MutationObserver, which is designed for that objective.
https://developer.mozilla/en-US/docs/DOM/MutationObserver
Sounds a lot like Live Query. Although this does involve continuous polling which does have a performance hit.
Here are some demos.
It looks like a job for the delegated form of the .on()
event:
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or
document
if the event handler wants to monitor all bubbling events in the document. Thedocument
element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.
本文标签: javascriptwatch for dom element creationStack Overflow
版权声明:本文标题:javascript - watch for dom element creation? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741419022a2377693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论