Jquery .each through Divs inside another Div

你。 提交于 2019-12-09 02:59:48

问题


I currently have the following 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'.

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

Any help appreciated.


回答1:


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

   });
})



回答2:


Is this what you're lookng for?

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

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




回答3:


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



回答4:


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

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



来源:https://stackoverflow.com/questions/11544072/jquery-each-through-divs-inside-another-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!