jQuery:获取选定的元素标记名称

编程入门 行业动态 更新时间:2024-10-24 04:43:31
本文介绍了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"

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

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:获取选定的元素标记名称

本文发布于:2023-11-12 12:40:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   元素   名称   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!