我有以下标记,其中包含 10 个pre
元素的类indent
:
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
我正在使用以下 jQuery.each()
函数来遍历每个元素:
$(function(){
$.each(".indent", function(index){
alert(index);
});
});
我希望看到 10 个警报,但我只看到 7 个
但是,这可以按预期使用$(".indent").each()
:
$(function(){
$(".indent").each(function(index){
alert(index);
});
});
查看$.each()
文档,我了解存在差异:
$.each() 函数与 $(selector).each() 不同,后者用于以独占方式迭代 jQuery 对象。
但我不明白为什么在这种情况下,它不会遍历所有元素。
为什么会这样?