[英]How can I listen for an element being inserted into the DOM with jQuery without using custom triggers?
I want to listen for another piece of code that inserts some html into the page.我想听另一段在页面中插入一些 html 的代码。 I don't control that code, so I can't do .trigger('custom_event')
and $(document).on('custom_event')
我不控制那个代码,所以我不能做.trigger('custom_event')
和$(document).on('custom_event')
Additionally, the inserted div that I want to listen for is nested inside of the inserted html.此外,我要监听的插入的 div 嵌套在插入的 html 中。
Here's what I have right now:这是我现在拥有的:
$(document).on('DOMNodeInsertedIntoDocument', "#inserted", function () {
console.log("Inserted into document:", $(this));
});
$(document).ready(function () {
var new_node = $('<div><div id="inserted">New Div</div></div>');
$("body").append(new_node);
});
http://jsfiddle.net/AeHxQ/5/ http://jsfiddle.net/AeHxQ/5/
Why doesn't the event get triggered?为什么没有触发事件?
You can use CSS animations with a little JavaScript, to detect when an element has been added to the DOM.您可以使用带有一点 JavaScript 的 CSS 动画来检测元素何时被添加到 DOM。 The technique is explained here: http://davidwalsh.name/detect-node-insertion该技术在这里解释:http: //davidwalsh.name/detect-node-insertion
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.