gpt4 book ai didi

javascript - 重新绑定(bind) onclick, on* 事件监听器进行调试

转载 作者:行者123 更新时间:2023-12-04 09:43:17 24 4
gpt4 key购买 nike

我想monkeypatch 事件监听器注册。

我找到了this answer显示如何为 addEventListener 执行此操作:

const nativeEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(...args) {
if (this.matches('div') && args[0] === 'click') {
console.log('listener is being added to a div');
debugger;
}
nativeEventListener.apply(this, args);
}

// then, when an event listener is added, you'll be able to intercept the call and debug it:
document.querySelector('div').addEventListener('click', () => {
console.log('clicked');
});


但这不包括 onclick , onkeydown等作业。

我不知道如何为那些做同样的事情,因为
const nativeOnClick = HTMLElement.prototype.onclick;

引发类型错误

TypeError: 'get onclick' called on an object that does not implement interface HTMLElement.


现在我想知道是否有一种特殊的方法可以专门检索 onclick 的 setter 和 getter等等,但到目前为止我的谷歌搜索没有运气。

最佳答案

您可以通过以下方式获得原始的 setter 函数:

const originalSetter = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'onchange').set;

如果你想重新定义一个属性,你应该看看使用 Object.defineProperty() .

关于javascript - 重新绑定(bind) onclick, on* 事件监听器进行调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62238373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com