开发者

Jquery - Finding what element $(this) is [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Can jQuery provide the tag name?

Hi!

This question is so 开发者_运维百科basic i am ashamed asking but i tried to find the answer for 30 minutes without any result.

How do i find out what kind of element has been clicked in the code below.

$('*').click(function (event) {
 var this_element = $(this).???;
 return false;
})

What i am looking for is to have the this_element variable set to 'a' if it's a link, 'p' if it's a paragraph 'div' if...

Thanks!


Try this:

$('*').click(function (event) {
    var this_element = this.tagName.toLowerCase();
    return false;
});

The this pointer refers to the actual element being acted upon. As part of the DOM Level 2 core, all DOM elements have a property called .tagName.


$(this).get(0).tagName;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜