2012-11-07 37 views
1
function redundantSee() { 

    var optionSet1 = $('.wrapper:eq(0)'), 
     optionSet2 = $('.wrapper:eq(1)'); 

    optionSet1.find('.options').each(function(){ 
     var self = $(this), 
      input = self.find('input'), 
      title = self.find('.title').text(), 
      value = input.val(), 
      source = self.find('img').attr('src'), 
      id = input.attr('id'), 
      listItem = $('<li/>', {'value': value, 'id': id }), 
      imageElement = $('<img/>', {'src': source, 'title': title}); 

     $('div.corresponding1').append(nameListItem); 
     listItem.append(imageElement); 
    }); 

    optionSet2.find('.options').each(function(){ 
     var self = $(this), 
      input = self.find('input'), 
      title = self.find('.title').text(), 
      value = input.val(), 
      source = self.find('img').attr('src'), 
      id = input.attr('id'), 
      listItem = $('<li/>', {'value': value, 'id': id }), 
      imageElement = $('<img/>', {'src': source, 'title': title}); 

     $('div.corresponding2').append(listItem); 
     listItem.append(imageElement); 
    }); 
} 

我已經簡化了這個帖子,因爲將要超過4個選項,通過設置此功能循環。有人可以幫我幹這些jQuery每個循環?

我在搞清楚如何把所有重複代碼的到的東西更易於管理的問題。

但由於每個選項組都有它自己的每個循環中,$(本)變量(以及所有相應的變量)是特定於在(」可供選項)元素上運行的循環。

如果我做一個每個迴路,並使用一個計數器,像這樣:

$('wrapper').each(function(i){ // ... }); 

我仍然會碰到需要重新聲明特定於optionSet輪到迴路中的所有我的新變量的問題。

有人可以幫我找出我如何可以凝聚這讓我不經常我每次添加設置功能的新選項時重複相同的代碼?

編輯:$(「div.corresponding」)元件是用於每一個完全不同的,所以它們不能用計數器遞增。 (例如一個可能是$( 'div.foo')或$( 'div.foo ul.bar')

+2

這屬於http://codereview.stackexchange.com/。 – jfriend00

+0

_ 「如果我每個循環都做一個循環並使用一個計數器......我仍然遇到需要重新聲明所有新的問題特定於該選項的變量Set循環「_ - 爲什麼?對於你顯示的代碼,不是''div.corresponding1''位是兩個循環的唯一區別嗎?你可以用櫃檯來處理。 – nnnnnn

+1

您應該創建一個重用的函數。該參數應該是$ ELEM($(本))和$容器($(「div.correspondingx」)) –

回答

2

例如,您可以延長jQuery的所以它的可重用性。

// extend jquery 
(function($){ 

    $.fn.redundantSee = function (subSelector) { 

     return this.find('.options').each(function() { 
      var self = $(this), 
      input = self.find('input'), 
      title = self.find('.title').text(), 
      value = input.val(), 
      source = self.find('img').attr('src'), 
      id = input.attr('id'), 
      listItem = $('<li/>', { 'value': value, 'id': id }), 
      imageElement = $('<img/>', { 'src': source, 'title': title }); 

      $(subSelector).append(listItem); 
      listItem.append(imageElement); 
     }); 

    }; 

}(jQuery)); 

// and use it like this 
$('.wrapper:eq(0)').redundantSee('div.corresponding1'); 
$('.wrapper:eq(1)').redundantSee('div.corresponding2'); 
+0

這是一個很好的可能性...我會給它一個鏡頭! – laserface

+0

直到明天我才能夠測試它,但是,我熟悉擴展jQuery和編寫可重用函數來接受參數(這是我正在考慮解決此問題的方式)。我很確定我可以通過這種方式進行實施。我的問題是,是否在通過編寫可重用的參數化函數來擴展jQuery方面有任何性能優勢,還是大多數情況下鏈接和可重用性最好? – laserface

+0

更多關於最佳實踐,我不認爲有任何性能優勢。 – Marc

0

我會做包裝一個循環,並在每個包裝 選擇一個循環,並我會用一個增量變量對應的DIV。

function redundantSee() { 
    var i = 0; 
    $('.wrapper').each(function(){ 
     i++; 
     $(this).find('.options').each(function(){ 
      var self = $(this), 
       input = self.find('input'), 
       title = self.find('.title').text(), 
       value = input.val(), 
       source = self.find('img').attr('src'), 
       id = input.attr('id'), 
       listItem = $('<li/>', {'value': value, 'id': id }), 
       imageElement = $('<img/>', {'src': source, 'title': title}); 

      $('div.corresponding' + i).append(nameListItem); 
      listItem.append(imageElement); 
     }); 
    }); 
} 
+0

'div.corresponding'不是可以增加的東西,因爲每個人都有一個完全不同的元素,並且名稱不同......請諒解這一點。 – laserface

+0

@ user1798630如果您使用的是html5,您可以在每個包裝上附加data- *屬性來指定相應的DIV,而不是增加它。 – EmeraldCoder

+0

其實我無法觸及那些包裝......它們是由服務器生成的,所以我只能使用JavaScript進行操作 – laserface

相關問題