sách gpt4 ăn đã đi

javascript - 重写 appendChild()

In lại Tác giả: Walker 123 更新时间:2023-11-30 17:09:59 30 4
mua khóa gpt4 giày nike

我正在尝试覆盖 appendChild 方法,以便我可以控制动态创建的元素并在插入页面之前根据需要修改它们。我尝试了这个示例代码,只是为了看看它是否可以完成:

var f = Element.prototype.appendChild;
Element.prototype.appendChild = function(){f.apply(this, arguments); };

它适用于简单的示例。但是,当我尝试在这段代码之后加载 jquery 时:

 
var f = Element.prototype.appendChild;
Element.prototype.appendChild = function(){f.apply(this, arguments); };



我得到这个错误:

Uncaught TypeError: Cannot read property 'selected' of undefined jquery.min.js:4
Uncaught TypeError: Cannot read property 'appendChild' of undefined

那么,为什么它不适用于 jquery 呢?有没有安全的方法来覆盖 appendChild ?

câu trả lời hay nhất

不建议覆盖 native 函数,但如果您这样做,请务必返回附加元素,以防止使用 native “appendChild”函数返回值的代码出现问题:

window.callbackFunc = function(elem, args) {
// write some logic here
}
window.f = Element.prototype.appendChild;
Element.prototype.appendChild = function() {
window.callbackFunc.call(this, arguments);
return window.f.apply(this, arguments);
};

关于javascript - 重写 appendChild(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234489/

30 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress