gpt4 book ai didi

javascript - 记录事件 ID

转载 作者:行者123 更新时间:2023-11-28 18:01:53 24 4
gpt4 key购买 nike

我需要在事件对象中识别文档上的特定事件。例如

$(document).click(function () {
console.log(1)
});

$(document).click(function () {
console.log(2)
});

var events = $._data(document, "events");
console.log(events);

enter image description here

当我记录所有“点击”事件时,它们之间没有区别:

如何为每个事件添加自定义 ID?也许我可以使用命名空间或者我可以更改guid

我需要检查:“特定事件是否存在?”

最佳答案

您可以绑定(bind)参数并将其传递给回调监听器来识别类型

$(document).click(function (type) {
console.log(type)
}.bind(this,"i-am-click"));

$(document).click(function (type) {
console.log(type)
}.bind(this,"i-am-another-click"));

var events = $._data(document, "events");
console.log(events);

更新

要识别事件数组中的特定事件对象,您可以执行以下操作。

 $(document).click(
(function(){
var fn = function(){ // Your callback function
console.log('i-am-click');
};

fn.event_id=1; // Adding id to the callback.

return fn; // returning the function after adding id
})()
);

$(document).click(
(function(){
var fn = function(){ // Your callback function
console.log('i-am-another-click');
};

fn.event_id=2; // Adding id to the callback.

return fn; // returning the callback function after adding id
})()
);

var events = $._data(document, "events");

// Find the events in the event array using filter

// This will return an array of match event with id in events array
events.click.filter(function(ev){return ev.handler.event_id==1;}); // event id you are looking for

关于javascript - 记录事件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43387419/

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