Javascript jQuery:获取所选元素标签名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5347357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:46:47  来源:igfitidea点击:

jQuery: Get selected element tag name

javascriptjqueryjquery-selectors

提问by configurator

Is there an easy way to get a tag name?

有没有一种简单的方法来获取标签名称?

For example, if I am given $('a')into a function, I want to get 'a'.

例如,如果我被赋予$('a')一个函数,我想得到'a'.

回答by tilleryj

You can call .prop("tagName"). Examples:

你可以打电话.prop("tagName")。例子:

jQuery("<a>").prop("tagName"); //==> "A"
jQuery("<h1>").prop("tagName"); //==> "H1"
jQuery("<coolTagName999>").prop("tagName"); //==> "COOLTAGNAME999"


If writing out .prop("tagName")is tedious, you can create a custom function like so:


如果写出来.prop("tagName")很乏味,你可以像这样创建一个自定义函数:

jQuery.fn.tagName = function() {
  return this.prop("tagName");
};

Examples:

例子:

jQuery("<a>").tagName(); //==> "A"
jQuery("<h1>").tagName(); //==> "H1"
jQuery("<coolTagName999>").tagName(); //==> "COOLTAGNAME999"


Note that tag names are, by convention, returned CAPITALIZED. If you want the returned tag name to be all lowercase, you can edit the custom function like so:


请注意,按照惯例,标签名称返回CAPITALIZED。如果您希望返回的标签名称全部为小写,您可以像这样编辑自定义函数:

jQuery.fn.tagNameLowerCase = function() {
  return this.prop("tagName").toLowerCase();
};

Examples:

例子:

jQuery("<a>").tagNameLowerCase(); //==> "a"
jQuery("<h1>").tagNameLowerCase(); //==> "h1"
jQuery("<coolTagName999>").tagNameLowerCase(); //==> "cooltagname999"

回答by SLaks

You can use the DOM's nodeNameproperty:

您可以使用 DOM 的nodeName属性

$(...)[0].nodeName

回答by Rob

As of jQuery 1.6 you should now call prop:

从 jQuery 1.6 开始,您现在应该调用 prop:

$target.prop("tagName")

See http://api.jquery.com/prop/

http://api.jquery.com/prop/

回答by Dayron Gallardo

jQuery 1.6+

jQuery 1.6+

jQuery('selector').prop("tagName").toLowerCase()

Older versions

旧版本

jQuery('selector').attr("tagName").toLowerCase()

toLowerCase() is not mandatory.

toLowerCase() 不是强制性的。

回答by Chepech

This is yet another way:

这是另一种方式:

$('selector')[0].tagName

回答by John Slegers

You should NOTuse jQuery('selector').attr("tagName").toLowerCase(), because it only works in older versions of Jquery.

您应使用jQuery('selector').attr("tagName").toLowerCase(),因为它只能在旧版本的jQuery工作。

You coulduse $('selector').prop("tagName").toLowerCase()if you're certain that you're using a version of jQuery thats >= version 1.6.

可以使用$('selector').prop("tagName").toLowerCase(),如果你确信你使用jQuery的版本,多数民众赞成在> = 1.6版。



Note :

笔记 :

You may think that EVERYONE is using jQuery 1.10+ or something by now (January 2016), but unfortunately that isn't really the case. For example, many people today are still using Drupal 7, and every official release of Drupal 7 to this day includes jQuery 1.4.4 by default.

您可能认为现在(2016 年 1 月)每个人都在使用 jQuery 1.10+ 或其他版本,但不幸的是,事实并非如此。例如,今天很多人仍在使用 Drupal 7,并且直到今天,Drupal 7 的每个官方版本都默认包含 jQuery 1.4.4。

So if do not know for certain if your project will be using jQuery 1.6+, consider using one of the options that work for ALL versions of jQuery :

因此,如果不确定您的项目是否将使用 jQuery 1.6+,请考虑使用适用于所有 jQuery 版本的选项之一:

Option 1 :

选项1 :

jQuery('selector')[0].tagName.toLowerCase()

Option 2

选项 2

jQuery('selector')[0].nodeName.toLowerCase()

回答by Donovan P

nodeName will give you the tag name in uppercase, while localName will give you the lower case.

nodeName 会给你大写的标签名称,而 localName 会给你小写。

$("yourelement")[0].localName 

will give you : yourelement instead of YOURELEMENT

会给你: yourelement 而不是 YOURELEMENT