如何在jQuery中选择具有特定ID的所有元素?

时间:2022-07-19 09:32:15

I'm trying to select all <div>s with the same ID in jQuery. How do i do it?

我试着在jQuery中选择所有

s和相同的ID。我该怎么做呢?

I tried this and it did not work

我试过了,但没有成功

jQuery('#xx').each(function(ind,obj){
      //do stuff;
});

7 个解决方案

#1


38  

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:

好吧,给每个倒计时计时器添加多个类怎么样?即:

<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'

这样你就能两全其美。它甚至允许重复“id”

#2


376  

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. id in 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. div in your case)

这当然适用于任何属性的选择,并且您可以通过指定问题中的标记(例如,在您的例子中是div)进一步细化您的选择。

jQuery("div[id=elemid]") 

#3


36  

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')来同时选择它们。

#4


4  

$("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 +“]”)将返回所有具有相同名称的控件,但您需要确保文本不应该出现在任何的控制

#5


2  

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元素。

#6


2  

Try this for selecting div by first id

尝试按第一个id选择div

$('div[id^="c-"]')

#7


1  

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")

您还可以尝试使用唯一id将两个div包装在两个div中。然后用$(“#div1”,“#wraper1”)和$(“#div1”,“#wraper2”)选择div。

Here you go:

给你:

<div id="wraper1">
<div id="div1">
</div>
<div id="wraper2">
<div id="div1">
</div>

#1


38  

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:

好吧,给每个倒计时计时器添加多个类怎么样?即:

<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'

这样你就能两全其美。它甚至允许重复“id”

#2


376  

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. id in 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. div in your case)

这当然适用于任何属性的选择,并且您可以通过指定问题中的标记(例如,在您的例子中是div)进一步细化您的选择。

jQuery("div[id=elemid]") 

#3


36  

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')来同时选择它们。

#4


4  

$("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 +“]”)将返回所有具有相同名称的控件,但您需要确保文本不应该出现在任何的控制

#5


2  

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元素。

#6


2  

Try this for selecting div by first id

尝试按第一个id选择div

$('div[id^="c-"]')

#7


1  

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")

您还可以尝试使用唯一id将两个div包装在两个div中。然后用$(“#div1”,“#wraper1”)和$(“#div1”,“#wraper2”)选择div。

Here you go:

给你:

<div id="wraper1">
<div id="div1">
</div>
<div id="wraper2">
<div id="div1">
</div>