在jQuery中选择两个具有相同id的不同HTML元素类型

时间:2020-12-31 00:51:31

I have two different elements with the same id. How do I select the div element with id="abc" in jQuery.

我有两个具有相同ID的不同元素。如何在jQuery中选择id =“abc”的div元素。

For example:

例如:

<select id="abc">

  <option>jhon</option>

  <option>Richard</option>

</select>

<div id="abc">

</div>

3 个解决方案

#1


3  

It is given, that you cannot have multiple identic ID on same page.

给出的是,您不能在同一页面上拥有多个identic ID。

The page won't be (X)HTML Valid DOM Document

该页面不是(X)HTML有效DOM文档

If you do it, then you can't find elements by their id using javascript call getElementById(String)

如果你这样做,那么你无法使用javascript调用getElementById(String)找到他们的id元素

See references:

见参考文献:


For targeting multiple DIV elements on page, use .class instead

要在页面上定位多个DIV元素,请改用.class

<select class="abc">
    <option>jhon</option>
    <option>Richard</option>
</select>
<div class="abc">

</div>

and find them by jQuery library

并通过jQuery库找到它们

var divCollection = $(".abc");

#2


0  

You CAN have multiple id's you should try to select it with the type of element you want $('select#abc') or $('div#abc'). You should never do it but its possible. I mean the word IDENTIFIER says it all or ;-) ?

你可以有多个id,你应该尝试用你想要的元素类型('select#abc')或$('div#abc')来选择它。你永远不应该这样做,但它可能。我的意思是IDENTIFIER这个词说的全部或者;-)?

#3


0  

When you try to just use javascript or jquery only the first id that is first select element will be taken and not the second one.

当你尝试只使用javascript或jquery时,第一个id将是第一个选择元素,而不是第二个。

#1


3  

It is given, that you cannot have multiple identic ID on same page.

给出的是,您不能在同一页面上拥有多个identic ID。

The page won't be (X)HTML Valid DOM Document

该页面不是(X)HTML有效DOM文档

If you do it, then you can't find elements by their id using javascript call getElementById(String)

如果你这样做,那么你无法使用javascript调用getElementById(String)找到他们的id元素

See references:

见参考文献:


For targeting multiple DIV elements on page, use .class instead

要在页面上定位多个DIV元素,请改用.class

<select class="abc">
    <option>jhon</option>
    <option>Richard</option>
</select>
<div class="abc">

</div>

and find them by jQuery library

并通过jQuery库找到它们

var divCollection = $(".abc");

#2


0  

You CAN have multiple id's you should try to select it with the type of element you want $('select#abc') or $('div#abc'). You should never do it but its possible. I mean the word IDENTIFIER says it all or ;-) ?

你可以有多个id,你应该尝试用你想要的元素类型('select#abc')或$('div#abc')来选择它。你永远不应该这样做,但它可能。我的意思是IDENTIFIER这个词说的全部或者;-)?

#3


0  

When you try to just use javascript or jquery only the first id that is first select element will be taken and not the second one.

当你尝试只使用javascript或jquery时,第一个id将是第一个选择元素,而不是第二个。