Jquery嵌套foreach

时间:2015-09-08 06:57:46

标签: javascript jquery foreach

G'Day伙计们, 我在理解Jquery中的foreach循环时遇到问题,尤其是嵌套循环。我想对每个元素进行编号,并在foreach循环中使用onclick方法调用它们。我尝试了很多,但没有尝试过的东西。:(

https://jsfiddle.net/sozf99a6/

HTML Structure
<div class='object'>
<div class='images'>
    <div class='bigImage'><img src=/></div>
    <div class='previewImage'><img src=/></div>
    <div class='bigImage'><img src=/></div>
    <div class='previewImage'><img src=/></div>
</div>
<div class='description'>blablabla</div>
<div class='description'>blablabla2</div>
<div class='description'>blablabla3</div>

Javascript
     //This function numbers every item to call it onclick
//each previewImage and description is assigned to one bigImage
$(function(){

  $('.object').each(function(i){
  $(this).addClass('slider' + (i+1));

  $('.bigImage').each(function(i){
  $(this).addClass('product' + (i+1));

   $('.previewImage').each(function(i){
  $(this).addClass('navImage' + (i+1));

   $('.description').each(function(i){
  $(this).addClass('slidetext' + (i+1));

});});
      /*
========^
    How it should works technically-
I)   
  $('.object').each(function(i){
     $(this).addClass('slider' + (i+1)).each(function(i){
      $('.bigImage').each(function(i){
              $(this).addClass('product' + (i+1));
              $('.previewImage').each(function(i){
              $(this).addClass('navImage' + (i+1));
              $('.description').each(function(i){
              $(this).addClass('slidetext' + (i+1));}
              });

 II)
    var i=0;
    $('.navImage'+('+i+')').click(function(i){
     $('.bigImage').hide();
     $('.product'+('+i+')').show();
     $('.description').hide();
     $('.slidetext'+('+i+')').show();  
   i++;
    });


*/    

感谢您帮助我 -

0 个答案:

没有答案