gpt4 book ai didi

javascript - 如何检查在jquery中点击了什么?

转载 作者:行者123 更新时间:2023-11-30 12:35:08 25 4
gpt4 key购买 nike

我想检查整个文档点击了什么,并将此信息保存到目标变量。

我想用点击目标的知识做一些逻辑。

示例代码如下:

$(document).ready(function(){
console.log("Document is Ready");
event = "";
previousEvent = "";
function init()
{
event = "";
target = "";
} init();
function loop()
{
if(event==previousEvent) {event="EVENT_NOTHING"; }
if(event!=previousEvent&&event!="EVENT_NOTHING") {
console.log("EVENT: " + event);
// (...) some logic (...)
}

previousEvent = event;
}
loop = setInterval(loop,100);

//EVENTS
$(document).keydown(function(e){
var key = e.which;
if(key == "37") event = "EVENT_KEYPRESS_LEFTARROW";
else if(key == "38") event = "EVENT_KEYPRESS_UPARROW";
else if(key == "39") event = "EVENT_KEYPRESS_RIGHTARROW";
else if(key == "40") event = "EVENT_KEYPRESS_DOWNARROW";
else {event = "EVENT_KEYPRESS_UNBINDED";}
});

$(document).click(function(){
event = "EVENT_KEYPRESS_CLICK";
target = ???????; //for example element in canvas, or any tag in html
});
});

最佳答案

事件有一个目标属性

$(document).click(function(e){
event = "EVENT_KEYPRESS_CLICK";
target = e.target;
});

关于javascript - 如何检查在jquery中点击了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26293332/

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