[英]Can I create a DOM element without it being in the DOM using jQuery?
[英]How can I listen for an element being inserted into the DOM with jQuery without using custom triggers?
我想聽另一段在頁面中插入一些 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);
});
為什么沒有觸發事件?
您可以使用帶有一點 JavaScript 的 CSS 動畫來檢測元素何時被添加到 DOM。 該技術在這里解釋:http: //davidwalsh.name/detect-node-insertion
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.