2017-04-19 1 views
0

Iは、よりエレガントな解決策に次のように変換しようとしている、おそらくループを使用Swiper.jsの弾丸に配列を追加しようとすると:ループ

jQuery(document).ready(function() { 
var swiper = new Swiper('.swiper-container', { 
    pagination: '.swiper-pagination', 
    paginationClickable: true, 
    paginationBulletRender: function (swiper, index, className) { 
     var index; 
     var a = ['Item One', 'Item Two', 'Item Three', 'Item Four', 'Item Five']; 
     if (index === 0) { 
      return '<span class="' + className + '">' + (a[0]) + '</span>'; 
     } 
     if (index === 1) { 
      return '<span class="' + className + '">' + (a[1]) + '</span>'; 
     } 
     if (index === 2) { 
      return '<span class="' + className + '">' + (a[2]) + '</span>'; 
     } 
     if (index === 3) { 
      return '<span class="' + className + '">' + (a[3]) + '</span>'; 
     } 
     if (index === 4) { 
      return '<span class="' + className + '">' + (a[4]) + '</span>'; 
     } 
     if (index === 5) { 
      return '<span class="' + className + '">' + (a[5]) + '</span>'; 
     } 
    } 
}); 

}); 

Iのような様々なループの手法を試みましたthis SO post中のもの、特にこの1:ループとしてこれを作成

var index; 
var a = ["a", "b", "c"]; 
for (index = 0; index < a.length; ++index) { 
    console.log(a[index]); 
} 

jQuery(document).ready(function() { 
    var swiper = new Swiper('.swiper-container', { 
     pagination: '.swiper-pagination', 
     paginationClickable: true, 
     paginationBulletRender: function (swiper, index, className) { 
      var index; 
      var a = ['Item One', 'Item Two', 'Item Three', 'Item Four', 'Item Five']; 
      for (index = 0; index < a.length; ++index) { 
       return '<span class="' + className + '">' + (a[index]) + '</span>'; 
      } 
     } 
    }); 

});

しかし、これは各弾丸の配列の最初の項目を表示するだけです。私はJS Fiddle hereを作成しました。

これはプロトタイプ専用ですので、配列は変更されません。誰かが助けてくれますか?

ありがとうございます!自身Swiper

答えて

0

は5回繰り返されて、あなたは上のチェックソリューション>>優れたhttps://jsfiddle.net/nv0mfy0z/1/

var a = ['Item One', 'Item Two', 'Item Three', 'Item Four', 'Item Five']; 

paginationBulletRender: function (swiper, index, className) { 
     return '<span class="' + className + '">' + (a[index]) + '</span>';    
    } 
+0

を反復するために、再びループのために必要はありません!ありがとうございました :) – user3327812