我需要获得具有给定id的所有div,但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>
Run Code Online (Sandbox Code Playgroud)
脚本:
$("#historial").each(function() {
alert("one div");
});
Run Code Online (Sandbox Code Playgroud)
如果我传递一个锚点oa id + anchor ej $("#lala a")它可以正常工作.
怎么了?
BR
Guf*_*ffa 13
您只能对页面中的一个元素使用特定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");
});
Run Code Online (Sandbox Code Playgroud)
ID应该是唯一的,页面上应该只有一个具有特定ID的元素.
如果您需要对这些DIV进行分组,请改用"class".
<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>
Run Code Online (Sandbox Code Playgroud)
所以修改后的jQuery,用类'historal'查找每个DIV看起来像这样:
$("div.historal").each(function() {
alert($(this).text()); //Prints out the text contained in this DIV
});
Run Code Online (Sandbox Code Playgroud)
另外,旁注 - jQuery使用#,而不是HTML标记 - 例如,如果你有像DIV那样
<div id="historal">stuff</div>
Run Code Online (Sandbox Code Playgroud)
你会发现像这样的jQuery:
$("#historal")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13936 次 |
| 最近记录: |