简体   繁体   English

如何使元素(而不是目标)成为jQuery中的事件?

[英]How to get the element made (not target) the event in jQuery?

<a class='tagselector' style="width=100px;height:100px">
<div style="margin-top:12px;">
MyName
</div>  
</a>

In the above Mark up if I made a script as shown below (in ready() function) 在上面,标记是否按如下所示编写了脚本(在ready()函数中)

$(".tagselector").click(function(clickevt){
console.log($(clickevt.target));
});

$(clickevt.target) This will return either <div> or <a> Tag upon click. $(clickevt.target)单击时将返回<div><a>标记。 I want to get the tag that made this click. 我想获得点击的标签。

NB: Please edit the question if necessary! 注意:如有必要,请编辑问题!

i assume this is what you need 我认为这就是你所需要的

$(".tagselector").click(function(clickevt){
if (clickevt.target == this)
    //your stuff
});

fiddle here 在这里摆弄

Use currentTarget : 使用currentTarget

$(".tagselector").click(function(clickevt){
    console.log($(clickevt.currentTarget));
});

Or simply use this keyword in your script: 或者只是在脚本中使用this关键字:

$(".tagselector").click(function(clickevt){
    console.log($(this));
});

Try this: 尝试这个:

$(".tagselector").click(function(clickevt){
    console.log($(this));
});

As far as i understand your question..This probably would help : 据我了解您的问题。这可能会有所帮助:

$(".tagselector").click(function(clickevt){
    console.log($(this)[0].tagName; );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM