【问题标题】:Jquery .each through Divs inside another DivJquery .each 通过另一个 Div 中的 Div
【发布时间】:2012-07-17 15:32:55
【问题描述】:

我目前有以下html:

抱歉编辑,我写这篇文章时没有注意。

 <div class ="left">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>

我正在使用 JQuery .each 命令使用 $('div').each 遍历每个 div。虽然我只想用类名“left”遍历 div 内的 div。

我尝试过使用$('.left &gt; div').each,但没有成功。

任何帮助表示赞赏。

【问题讨论】:

标签: javascript jquery html each loops


【解决方案1】:

语法应该是$(".left &gt; div").each(function(){}); 而不是.each(".left &gt; div")

更新:想使用$(".left").children().each()

【讨论】:

  • 抱歉,我在写这篇文章的时候已经半睡半醒了。我使用正确。
  • 你发布的语法是我已经没有的,很遗憾:(
【解决方案2】:

这就是你要找的吗?

$('div.left>div').each(function(){ /* do stuff */ });

小提琴: http://jsfiddle.net/MLnBY/

【讨论】:

  • 它适用于这个例子,但在我的(简化的)代码中它变得更加复杂。如果这很重要,我的 div 会在表格中吗?
  • 啊,然后删除&gt;$('div.left div').each(...); 这将迭代所有具有 classname=left 的祖先 div 的 div。
【解决方案3】:
$.each( $('.left'), function(i, left) {
   $('div', left).each(function() {

   });
})

【讨论】:

  • 这就像倒着吃。不是最简单的解决方案,但它有效! :)
【解决方案4】:
$(".left").children().each(function(i, elm) {
    alert($(this).html())
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多