jquery访问所有具有id属性的元素(jquery accessing all elements who have id attribute)

编程入门 行业动态 更新时间:2024-10-27 04:24:15
jquery访问所有具有id属性的元素(jquery accessing all elements who have id attribute)

我想从一个div中拥有id attribute所有元素,意味着我想要执行如下操作:

$("div").find("*[id]").each(function() { alert(this.id) });

但这不起作用,有人可以帮我吗?

I want to get all elements from a div who have id attribute, means i want to do something like:

$("div").find("*[id]").each(function() { alert(this.id) });

but this is not working , can anybody help me pls?

最满意答案

你的代码工作得很好,但你可以从选择器中删除* 。

其他有效选项:

$("div").find("[id]").each(function() { alert(this.id) });

现场演示

或这个:

$("div *").filter("[id]").each(function() { alert(this.id) });

现场演示

或这个:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

现场演示

Your code works just fine, but you can remove the * from the selector.

Other valid options:

$("div").find("[id]").each(function() { alert(this.id) });

LIVE DEMO

Or this:

$("div *").filter("[id]").each(function() { alert(this.id) });

Live DEMO

Or this:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

Live DEMO

更多推荐

本文发布于:2023-08-07 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   元素   id   jquery   elements

发布评论

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

>www.elefans.com

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