我需要获得所有的div与给定的ID,但jquery的每个函数只有获得的第一个。

示例:

<div id="#historial">some html code</div>
<div id="#historial">some html code</div>
<div id="#historial">some html code</div>
<div id="#historial">some html code</div>

脚本:

$("#historial").each(function() {
alert("one div");
});

如果我通过锚o一个ID +锚EJ $( “#拉拉一”),它的工作正常。

怎么了?

BR

有帮助吗?

解决方案

您只能在页面中使用的特定ID为一个元素。使用一个类来代替:

<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>

$(".historial").each(function(e) {
  alert("one div");
});

其他提示

的ID应该是唯一的,应该只有在页面上的一个元件与一个特定ID。

如果需要基团的那些的DIV,使用“类”代替。

<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>

所以订正jQuery的,以查找与类“historal”各自DIV是这样的:

$("div.historal").each(function() {
    alert($(this).text());    //Prints out the text contained in this DIV
});

另外,sidenote-的#所使用的jQuery,而不是HTML markup-例如,如果你有像

一个DIV
<div id="historal">stuff</div>

您会发现,使用jQuery这样的:

$("#historal")
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top