开发者

How to get the name of the current selected HTML tag?

I have selected some tag with jQuery:

$('select, :checkbox, :radio').each(fu开发者_如何学Gonction(){
   // ...
});

Now, I need to get the name of the current tag:

$('select, :checkbox, :radio').each(function(){
   var tag_name = $(this). ???
   alert(tag_name);
});

Expected result: "select", "input" and so on.

So, I need to know, how to get the tag name of element. Maybe without jQuery, with native javascript functions - no matter how.


You can use the HTML DOM native tagName property. Try this:

var tag_name = this.tagName;


$('select, :checkbox, :radio').each(function(){
   var tag_name = this.tagName;
   alert(tag_name);
});


Just this.tagName will give you the node name.


Try this:

$('select, :checkbox, :radio').each(function(){
   alert($(this).get(0).nodeName);
});


sure... very simple

Here is a working example

http://jsfiddle.net/L96KG/

here is the reference source

Can jQuery provide the tag name?


You can also do:

$('select, :checkbox, :radio').each(function(el){
    alert(el.tagName);
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜