2011-02-08 79 views
3

我有一個超長的jquery片段,我覺得可以用正確的邏輯進行簡化。自從我終於開始工作以來,我很害怕接觸它。如何簡化if/else jquery片段

「else $ target = #none」實際上是「無顯示」語句。我不確定如何以更好的方式表達這一點。

非常感謝! -zeem

PS。鏈接到醫療大麻網站,所以NSFW鏈接!

$(function() { 
    var $target = $('#CO1'); 
    if (mmjsRegion == 'CO') { 
     $target = $('#CO1'); 
    } else { 
     $target = $('#none'); 
    } 
    $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CO1.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 

$(function() { 
    var $target = $('#CO2'); 
    if (mmjsRegion == 'CO') { 
     $target = $('#CO2'); 
    } else { 
     $target = $('#none'); 
    } 
    $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CO2.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 
$(function() { 
    var $target = $('#CO3'); 
    if (mmjsRegion == 'CO') { 
     $target = $('#CO3'); 
    } else { 
     $target = $('#none'); 
    } 
    $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CO3.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 
$(function() { 
    var $target = $('#CA1'); 
    if (mmjsRegion == 'CA') { 
     $target = $('#CA1'); 
    } else { 
     $target = $('#none'); 
    } 
    $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CA1.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 
$(function() { 
    var $target = $('#CA2'); 
    if (mmjsRegion == 'CA') { 
     $target = $('#CA2'); 
    } else { 
     $target = $('#none'); 
    } 
    $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CA2.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 
$(function() { 
    var $target = $('#CA3'); 
    if (mmjsRegion == 'CA') { 
     $target = $('#CA3'); 
    } else { 
     $target = $('#none'); 
    } 
    $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CA3.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 

回答

2

這應做到:

var r = mmjsRegion, 
    s = r == 'CO' ? '#CO1, #CO2, #CO3' : r == 'CA' ? '#CA1, #CA2, #CA3' : ''; 

$(s).each(function() { 
    $(this).imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + this.id + '.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
    }); 
}); 
2
$(["CO;CO1", "CO;CO2", "CO;CO3", "CA;CA1", "CA;CA2", "CA;CA3"]).each(function() { 

     var data = this.split(";"); 
     var id = data[1]; 
     var region = data[0]; 

     var $target = $("#" + id); 
     if (mmjsRegion == region){ 
     $target = $("#" + id); 
     } 
     else{ 
      $target = $('#none'); 
     } 

     $target.imBannerRotater({ 
     return_type: 'json', 
     data_map: { 
      image_name: 'name', 
      url_name: 'url' 
     }, 
     image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + id + '.php', 
     base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
     }); 

}); 

我沒有看到這樣做的原因..

var $target = $('#CO1'); 
     if (mmjsRegion == 'CO'){ 
     $target = $('#CO1'); 
     } 
     else{ 
      $target = $('#none'); 
     } 
+0

你,我做到了,因爲我無法弄清楚是怎麼回事,以寫else不要運行功能。 感謝您的回覆!可悲的是,當我將它輸入到網站時,它不起作用。 – zeemy23 2011-02-08 00:46:40

2

權,我認爲可以歸結爲:

(function($) { 
    $.each(['CO1', 'CO2', 'CO3', 'CA1', 'CA2', 'CA3'], function(index, id) { 
     $('#' + (mmjsRegion == id.replace(/\d+$/,'') ? id : 'none')).imBannerRotater({ 
      return_type: 'json', 
      data_map: { 
       image_name: 'name', 
       url_name: 'url' 
      }, 
      image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + id + '.php', 
      base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
     }); 
    }); 
})(jQuery); 

由於mmjsRegion過程中不會改變代碼,它可以降低更多,但我不知道imBannerRotate()插件應該是調用編輯$('#none')三次,看起來像一個黑客。如果不需要$('#none')那麼就可以:

(function($) { 
    $.each(['1', '2', '3'], function(index, id) { 
     $('#' + mmjsRegion + id).imBannerRotater({ 
      return_type: 'json', 
      data_map: { 
       image_name: 'name', 
       url_name: 'url' 
      }, 
      image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + mmjsRegion + id + '.php', 
      base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/', 
     }); 
    }); 
})(jQuery); 
+0

是啊!爲什麼我也沒有看到這個 – Luke 2011-02-08 00:47:36

+0

可悲的是這段代碼沒有工作。感謝您的輸入! – zeemy23 2011-02-08 00:53:33

+0

@ zeemy23:沒有工作怎麼樣? – Orbling 2011-02-08 01:01:12

2
$(function(){ 

    var targets = ['CO', 'CA']; 
    var iterations = 3; 

    $.each(targets, function(index, value){ 

     for(var i=1; i<=iterations; i++) 
     { 
      var targetId = '#' + value + i.toString(); 
      $target = $(targetId); 

      if (mmjsRegion == value) { 
       $target = $(targetId); 
      } else { 
       $target = $('#none'); 
      } 

      $target.imBannerRotater({ 
       return_type: 'json', 
       data_map: { 
        image_name: 'name', 
        url_name: 'url' 
       }, 
       image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + value + i.toString() + '.php', 
       base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/' 
      }); 

     } 
    }); 
});