Javascript 获取元素的 `id` 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7753560/
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
Getting an element's `id` attribute
提问by justin
Can you get the id
attribute of a html tag using jQuery or without?
您可以id
使用 jQuery 或不使用 jQuery获取html 标签的属性吗?
For example:
例如:
<ul id="todo" />
How can I get the id
, without using jQuery("#todo")
?
我怎样才能得到id
, 而不使用jQuery("#todo")
?
Is there a way for that? Will attr()
work?
有办法吗?会attr()
工作吗?
回答by alex
You can use attr('id')
in jQuery or the id
property (or getAttribute('id')
) on the native DOM element.
您可以attr('id')
在 jQuery 或本机 DOM 元素上的id
属性(或getAttribute('id')
)中使用。
回答by John Gathogo
The way I see it, you most probably need to attach a selector class to the ulso that you can use it to get the id:
在我看来,您很可能需要将一个选择器类附加到ul 上,以便您可以使用它来获取 id:
<ul id="todo" class="todoclassname" />
then, you can get the id by doing:
然后,您可以通过执行以下操作来获取 id:
$(".todoclassname").attr("id");
回答by Geoff
Yes, you can use .attr('id')
to get the id of an element
是的,您可以使用.attr('id')
来获取元素的 id