我想检查点击整个文档的内容并将此信息保存到目标变量。
我想了解一下有关点击目标的知识。
以下是示例代码:
$(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
});
});
答案 0 :(得分:4)
事件具有目标属性
$(document).click(function(e){
event = "EVENT_KEYPRESS_CLICK";
target = e.target;
});
答案 1 :(得分:-1)
您可以将jquery事件中的目标设为$(this)
。您可以在此对象上使用任何jQuery函数。
$(document).click(function(e){
var event = "EVENT_KEYPRESS_CLICK";
var target = $(this);
});