[英]How do I grab (and use) the element being clicked in javascript/jQuery?
So basically what I'm trying to do is apply one click event, and then using event.target determine exactly what was clicked on. 因此,基本上我想做的是应用一次单击事件,然后使用event.target确定确切单击了什么。 After determining what was clicked on, I can then use that to manipulate the DOM.
确定单击了什么之后,然后可以使用它来操作DOM。 Below is the closest I could get to giving me something somewhat usable, but it doesn't give me 'exactly what I clicked on'.
以下是我能给我的一些可用的最接近的信息,但是它并没有给我“我所点击的内容”。
$("body").click(function(event) { alert(event.target.nodeName); // Alerts the type of element you clicked on, but doesn't return anything 'usable' to manipulate the DOM });
for jQuery, $(event.target)
will give you a jQuery object containing your clicked element. 对于jQuery,
$(event.target)
将为您提供一个包含单击元素的jQuery对象。 Then you can manipulate to your hearts content. 然后,您可以操纵自己内心的满足感。
I see that your binding the click()
event to $('body')
So when you click the body of your document, click
is fired with e.target
being the body . 我看到您将
click()
事件绑定到$('body')
因此,当您单击文档的正文时,将以e.target
为body触发click
。 You probably want to attach that click
event listener to a more specific element, see: http://jsfiddle.net/nn4sJ/8/ for an example 您可能希望将
click
事件侦听器附加到更具体的元素,请参见: http : //jsfiddle.net/nn4sJ/8/作为示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.