gpt4 book ai didi

javascript - 事件从未触发?

转载 作者:行者123 更新时间:2023-12-02 19:15:19 26 4
gpt4 key购买 nike

我正在尝试向 document 添加一些事件监听器,但由于某种原因,click 事件似乎从未被触发,因为从未调用回调:

function NotJquery(element) {
this.element = element;

return this;
}

NotJquery.prototype.eventFired = function(event, callback) {
console.log('in NotJquery prototype');
return function (event, callback) {
element.addEventListener(event, callback, true);
};
};

var $ = function(element) {
return new NotJquery(element);
};

function Test() {
}

Test.prototype.addListener = function() {
console.log('in Test prototype');
$(document).eventFired('click', function() {
console.log('click event fired');
});
};

(function() {
var test= new Test();
test.addListener();
}());

消息:“在测试原型(prototype)中”和“在 NotJquery 原型(prototype)中”都记录在控制台中,但是当我单击文档中的某个位置时,消息“单击事件已触发”不会在控制台中输出。我看不出我的代码有什么问题。有人有办法让它发挥作用吗?

http://jsfiddle.net/Nn3tZ/1/

最佳答案

您的客户端代码期望如下所示:

NotJquery.prototype.eventFired = function(event, callback) {
this.element.addEventListener(event, callback, false);
};

工作演示:http://jsfiddle.net/Nn3tZ/2/

关于javascript - 事件从未触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209859/

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