Javascript Jquery .each 通过另一个 Div 中的 Div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11544072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:15:42  来源:igfitidea点击:

Jquery .each through Divs inside another Div

javascriptjqueryhtmleachloops

提问by James Mclaren

I currently have the following html:

我目前有以下 html:

Apologies for edit, I was not paying attention when i wrote this.

为编辑道歉,我写这篇文章的时候没有注意。

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

I am using the JQuery .each command to iterate through each div using $('div').each. Although I only want to iterate through the divs inside the div with the class name 'left'.

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

I have tried using $('.left > div').eachbut it did not work.

我试过使用,$('.left > div').each但没有用。

Any help appreciated.

任何帮助表示赞赏。

回答by thecodeparadox

$.each( $('.left'), function(i, left) {
   $('div', left).each(function() {

   });
})

回答by Paul Fleming

Is this what you're lookng for?

这是你要找的吗?

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

Fiddle: http://jsfiddle.net/MLnBY/

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

回答by ayyp

The syntax would be $(".left > div").each(function(){});instead of .each(".left > div").

语法将$(".left > div").each(function(){});代替.each(".left > div").

Update:Would want to use $(".left").children().each()

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

回答by alexl

$(".left").children().each(function(i, elm) {
    alert($(this).html())
});