javascript – 如何覆盖appendChild()?

前端之家收集整理的这篇文章主要介绍了javascript – 如何覆盖appendChild()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. appendChild = function(message) {
  2. console.log("intercepted!");
  3. }

使用上面的代码似乎不起作用.

有谁知道?

解决方法

您可能想要替换的是Element.prototype.appendChild,但这可能是一个坏主意.

此示例添加在inserted元素中截获的文本:

  1. var f = Element.prototype.appendChild;
  2. Element.prototype.appendChild = function(){f.apply(this,arguments);arguments[0].innerHTML="!Intercepted!"; };
  3. document.body.appendChild(document.createElement("div"));

Demonstration

猜你在找的JavaScript相关文章