【问题标题】:How to get the name of the current selected HTML tag?如何获取当前选中的 HTML 标签的名称?
【发布时间】:2011-10-13 17:04:43
【问题描述】:

我已经用 jQuery 选择了一些标签:

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

现在,我需要获取当前标签的名称:

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

预期结果:“选择”、“输入”等。

所以,我需要知道,如何获取元素的标签名称。也许没有 jQuery,使用原生 javascript 函数 - 无论如何。

【问题讨论】:

标签: javascript jquery tagname


【解决方案1】:

您可以使用 HTML DOM 原生的 tagName 属性。 试试这个:

var tag_name = this.tagName;

【讨论】:

【解决方案2】:
$('select, :checkbox, :radio').each(function(){
   var tag_name = this.tagName;
   alert(tag_name);
});

【讨论】:

    【解决方案3】:

    this.tagName 会给你节点名称。

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

      【解决方案5】:

      当然……很简单

      这是一个工作示例

      http://jsfiddle.net/L96KG/

      这里是参考来源

      Can jQuery provide the tag name?

      【讨论】:

        【解决方案6】:

        你也可以这样做:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-14
          • 1970-01-01
          • 1970-01-01
          • 2013-09-10
          • 2010-11-27
          • 2013-05-24
          • 2010-11-18
          • 2020-04-25
          相关资源
          最近更新 更多