jQuery:获取选定的元素标记名称 [英] jQuery: Get selected element tag name

查看:125
本文介绍了jQuery:获取选定的元素标记名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there an easy way to get a tag name?

例如,如果给我 $('a')进入一个函数,我想得到'a'

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

推荐答案

您可以调用 .prop(tagName)。示例:

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



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


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

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

示例:

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



请注意,按照惯例,标签名称已返回< b>用大写的。如果您希望返回的标记名称全部为小写,则可以编辑自定义函数,如下所示:


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:

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

示例:

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

这篇关于jQuery:获取选定的元素标记名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆