我想听一下在页面中插入一些html的另一段代码。我无法控制该代码,因此我无法执行.trigger('custom_event')
和$(document).on('custom_event')
此外,我想要侦听的插入div嵌套在插入的html中。
这就是我现在所拥有的:
$(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);
});
为什么事件不会被触发?
答案 0 :(得分:2)
您可以使用带有少量JavaScript的CSS动画来检测元素何时添加到DOM中。这里解释了这项技术:http://davidwalsh.name/detect-node-insertion