Possible Duplicate:
Can jQuery provide the tag name?
嗨!
这个问题太基础了,我不好意思问,但我尝试了 30 分钟寻找答案,但没有任何结果。
如何找出下面代码中单击了哪种元素。
$('*').click(function (event) {
var this_element = $(this).???;
return false;
})
我正在寻找的是,如果 this_element 变量是链接,则将其设置为“a”;如果是段落“div”,则将其设置为“p”...
谢谢!
最佳答案
试试这个:
$('*').click(function (event) {
var this_element = this.tagName.toLowerCase();
return false;
});
this
指针指的是正在执行操作的实际元素。作为 DOM Level 2 核心的一部分,所有 DOM 元素都有一个名为 .tagName
的属性。 .
关于Jquery - 查找 $(this) 是什么元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2330721/