我有一个超长的jquery片段,我觉得可以用正确的逻辑简化。自从我终于开始工作以来,我害怕接触它。
“else $ target = #none”实际上是一个“show nothing”语句。我不确定如何以更好的方式表达这一点。
非常感谢! -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/',
});
});
答案 0 :(得分: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');
}
答案 1 :(得分: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);
答案 2 :(得分: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/'
});
}
});
});
答案 3 :(得分: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/',
});
});