简体   繁体   English

如何获取(和使用)在javascript / jQuery中被单击的元素?

[英]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. 然后,您可以操纵自己内心的满足感。

--UPDATE --update

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.targetbody触发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.

相关问题 我如何获取被单击元素的类名并将其放入使用jQuery的函数中 - how do i grab the classname of a clicked element and put it in a function using jquery 如何获取包含Javascript / Jquery中单击文本的元素? - How do I get the element containing clicked text in Javascript/Jquery? Javascript / jQuery - 如何获取clicked元素类的名称? - Javascript/jQuery - How do I obtain name of the class of clicked element? javascript jquery访问被单击的元素 - javascript jquery accessing the element being clicked 如何在 Javascript 中获取构造为数组的表单中的 id 元素? - How do I grab an id element in a form that is constructed as an array in Javascript? 如何使用Jquery编写“如果未单击”或“如果在外部元素上单击”? - How do I write 'if not clicked' or 'if clicked outside element', using Jquery? jQuery发送到e,然后我可以使用e.currentTarget来获取页面上的元素。 我该如何使用它? - jQuery sends to e, which I can then use e.currentTarget to grab the element on the page. How do I then use it? 如何隐藏与没有jQuery时单击的元素最接近的元素? - How do I hide the closest element to the element that was clicked without jQuery? javascript / jquery-如何将对点击元素的引用传递给函数 - javascript / jquery - How do I pass a reference to my clicked element to my function 当使用Jquery / JavaScript单击上一个列表项时,如何定位嵌套在列表项内的元素? - How do I target an element nested inside a list item when the previous list item is clicked with Jquery/JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM