简体   繁体   中英

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

I have selected some tag with jQuery:

$('select, :checkbox, :radio').each(function(){
   // ...
});

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);
});

this.tagName会给您节点名称。

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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