我想监听另一段在页面中插入一些 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);
});
为什么事件没有被触发?
最佳答案
您可以使用 CSS 动画和一些 JavaScript 来检测元素何时添加到 DOM。该技术的解释如下:http://davidwalsh.name/detect-node-insertion
关于javascript - 如何在不使用自定义触发器的情况下使用 jQuery 监听插入到 DOM 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14818875/