如何枚举所有具有相同id的div标签?
我有基于小部件的网页,并且我为所有 div 赋予了相同的 ID 属性。 页面加载后,我想枚举与 ID 元素“widget”匹配的所有 div 元素。我正在使用 jQuery。
然后我想获取“widget”div 中的内部 div 属性,该属性将用作工具提示。
<div id="widget" class="span-8 " >
<h2><a href="">Example.com</a></h2>
<ul>
<li><h3><a href="example.com">Example News 1</a></h3>
<div id="tooltip-content">
<div class="published">Thu Jul 8, 2010</div>
<div class="content">
This detail news 1 shown only on tooltip..
</div>
</div>
</li>
<li><h3><a href="example.com">Example News 2</a></h3>
<div id="tooltip-content">
<div class="published">Thu Jul 8, 2010</div>
<div class="content">
This detail news 2 shown only on tooltip..
</div>
</div>
</li>
</ul>
基本上我想获取 id 为“widget”的所有小部件(每页大约有 20 个小部件),并获取 id 与小部件中的“tooltip-content”匹配的所有 div。一个小部件有 5 或 6 个工具提示内容。你能帮我枚举一下div吗?
I have web-pages based on widget and I have given all the div the same ID attribute.
After the page is loaded, I want to enumerate all the div element which matches the ID element 'widget'. I am using jQuery.
Then I want to get the internal div attribute within the 'widget' div which shall be used as a tooltip.
<div id="widget" class="span-8 " >
<h2><a href="">Example.com</a></h2>
<ul>
<li><h3><a href="example.com">Example News 1</a></h3>
<div id="tooltip-content">
<div class="published">Thu Jul 8, 2010</div>
<div class="content">
This detail news 1 shown only on tooltip..
</div>
</div>
</li>
<li><h3><a href="example.com">Example News 2</a></h3>
<div id="tooltip-content">
<div class="published">Thu Jul 8, 2010</div>
<div class="content">
This detail news 2 shown only on tooltip..
</div>
</div>
</li>
</ul>
Basically I want to get the all the widgets with id 'widget'( I have around 20 widgets per page) and get all the div which id matches 'tooltip-content' within widget. One widget has 5 or 6 tooltip-content. Can you help me to enumerate the divs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ID 在文档中应该是唯一的;每个 ID 只能有一个元素。请参阅 w3schools
您想要做的是在每个元素上使用
class
属性,然后根据类名称进行选择。如果您使用的是 Prototype、jQuery 或 Ext(或您喜欢的其他框架)等框架,则应该有一种通过 CSS 选择器或类名选择元素的方法。我建议您向每个元素添加一个名为widget
的类,并为它们提供唯一的 ID,IDs are supposed to be unique within a document; you are only supposed to have one element per ID. See w3schools
What you want to do is use a
class
attribute on each of those elements, and then select based on a class name. If you are using a framework such as Prototype, jQuery or Ext (or another one that you prefer), there should be a method to select elements by CSS selector or by class name. I recommend you add a class calledwidget
to each of those elements, and give them unique IDs instead,这是你的问题。根据定义,id 必须是唯一的。如果它不是唯一的,那么您会遇到各种各样的问题(例如,无法轻松枚举它们)。相反,您可以使用
class
属性(单个元素可以通过用空格分隔多个类来拥有多个类,因此class="widget span-8"
就可以了)。然后,您可以使用 jQuery 的基于类的选择器来轻松枚举:
This is your problem. The id, by definition, needs to be unique. If it's not unique then you run into all sorts of problems (like not being able to enumerate them easily, for example). Instead, you can use the
class
attribute (a single element can have multiple classes by separating them with spaces soclass="widget span-8"
is fine).You could then use jQuery's class-based selector for easy enumeration:
从技术上讲,元素 id 应该始终是唯一的,是的,您可以拥有具有相同 id 的元素,但您会遇到这样的问题。 jQuery 的选择器引擎只期望一个具有给定 id 的元素,因此最终会发生的情况是它将找到第一个元素并忽略其余元素。
你应该做的是给它们所有相同的类,例如
选择器将是
编辑:啊是的,出于某种原因我以为你提到你正在使用 jQuery。但这里有一个函数,可以通过 ID 获取所有元素,不需要任何库
Technically element id's should always be unique, yes you can have elements with the same id but you'll run into problems like this. Where jQuery's selector engine is only expecting one element with a given id, so what will end up happening is it will find the first element and ignore the rest.
What you should do is give them all the same class eg
and the the selector would be
EDIT: ah yeah for some reason I thought you mentioned you were using jQuery. but here's a function that will get all elements by ID that doesn't require any library's
大多数网络浏览器将“id”属性作为特殊属性处理,该属性意味着单个哈希查找。您不应将相同的 id 分配给多个元素。
我建议分配一个不同的属性,也许是“widget_id”。
完成此操作后,您可以通过以下方式枚举具有给定“widget_id”的所有元素:
Most web browsers handle the "id" attribute as a special attribute which is meant to be a single hash lookup. You shouldn't assign the same id to multiple elements.
I'd recommend assigning a different attribute, perhaps "widget_id".
Once you do that, you can enumerate all elements with a given "widget_id" via: