可能重复:
检测/监控 DOM 更改的最有效方法?
我试过这个,但它不起作用:
HTMLElement.prototype.onload = function() {
alert(this.tagName);
};
可能重复:
检测/监控 DOM 更改的最有效方法?
我试过这个,但它不起作用:
HTMLElement.prototype.onload = function() {
alert(this.tagName);
};
除非您将元素放在那里,否则元素不会出现在文档中。
存在突变事件以捕获插入、删除等,但它们没有得到很好的支持,因此大多数情况下最好在执行插入时捕获插入。
Really, what are you trying to achieve here?
If it is only about to determine if a given HTMLElement is currently in the DOM you can refer to the element's parentNode
attribute. All elements except <html>
will have a parentNode when they are part of the DOM tree. And you can have elements that you created via JS but never inserted into the DOM or elements that were in the DOM but now are removed from it. Either way, one with parentNode IS currently in the DOM and one w/o is not.