jQuery 如何在jQuery中选择具有特定ID的所有元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/902839/
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
How to select all elements with a particular ID in jQuery?
提问by mark
I'm trying to select all <div>
s with the same ID in jQuery. How do i do it?
我试图<div>
在 jQuery 中选择所有具有相同 ID 的 s。我该怎么做?
I tried this and it did not work
我试过这个,但没有用
jQuery('#xx').each(function(ind,obj){
//do stuff;
});
采纳答案by Ballsacian1
I would use Different IDs but assign each DIV the same class.
我会使用不同的 ID,但为每个 DIV 分配相同的类。
<div id="c-1" class="countdown"></div>
<div id="c-2" class="countdown"></div>
This also has the added benefit of being able to reconstruct the IDs based off of the return of jQuery('.countdown').length
这还有一个额外的好处,即能够根据 jQuery('.countdown').length 的返回值重建 ID。
Ok what about adding multiple classes to each countdown timer. IE:
好的,向每个倒数计时器添加多个类怎么样。IE:
<div class="countdown c-1"></div>
<div class="countdown c-2"></div>
<div class="countdown c-1"></div>
That way you get the best of both worlds. It even allows repeat 'IDS'
这样你就可以两全其美。它甚至允许重复“IDS”
回答by mydoghasworms
Though there are other correct answers here (such as using classes), from an academic point of view it is of course possible to have multiple divs with the same ID, and it is possible to select them with jQuery.
尽管这里还有其他正确答案(例如使用类),但从学术角度来看,当然可以有多个具有相同 ID 的 div,并且可以使用 jQuery 选择它们。
When you use
当你使用
jQuery("#elemid")
it selects only the first element with the given ID.
它只选择具有给定 ID 的第一个元素。
However, when you select by attribute (e.g. idin your case), it returns all matching elements, like so:
但是,当您按属性选择时(例如id在您的情况下),它会返回所有匹配的元素,如下所示:
jQuery("[id=elemid]")
This of course works for selection on any attribute, and you could further refine your selection by specifying the tag in question (e.g. divin your case)
这当然适用于任何属性的选择,您可以通过指定相关标签来进一步优化您的选择(例如div在您的情况下)
jQuery("div[id=elemid]")
回答by Ayman Hourieh
Your document should not contain two divs with the same id. This is invalid HTML, and as a result, the underlying DOM API does not support it.
您的文档不应包含两个具有相同 ID 的 div。这是无效的 HTML,因此底层 DOM API 不支持它。
From the HTML standard:
从HTML 标准:
id = name [CS] This attribute assigns a name to an element. This name must be unique in a document.
id = name [CS] 此属性为元素分配名称。此名称在文档中必须是唯一的。
You can either assign different ids to each div and select them both using $('#id1, #id2)
. Or assign the same class to both elements (.cls
for example), and use $('.cls')
to select them both.
您可以为每个 div 分配不同的 id 并使用$('#id1, #id2)
. 或者为两个元素分配相同的类(.cls
例如),并使用$('.cls')
来选择它们。
回答by Niranjan Kumar
$("div[id^=" + controlid + "]")
will return all the controls with the same name but you need to ensure that the text should not present in any of the controls
$("div[id^=" + controlid + "]")
将返回所有具有相同名称的控件,但您需要确保文本不应出现在任何控件中
回答by atabak
Try this for selecting div by first id
试试这个按第一个 id 选择 div
$('div[id^="c-"]')
回答by Paul Morie
Can you assign a unique CSS class to each distinct timer? That way you could use the selector for the CSS class, which would work fine with multiple div
elements.
您可以为每个不同的计时器分配一个唯一的 CSS 类吗?这样你就可以使用 CSS 类的选择器,它可以很好地处理多个div
元素。
回答by omer
You could also try wrapping the two div's in two div's with unique ids. Then select the div by $("#div1","#wraper1")
and $("#div1","#wraper2")
您还可以尝试将两个 div 包装在具有唯一 ID 的两个 div 中。然后通过$("#div1","#wraper1")
和选择div$("#div1","#wraper2")
Here you go:
干得好:
<div id="wraper1">
<div id="div1">
</div>
<div id="wraper2">
<div id="div1">
</div>