编程语言
首页 > 编程语言> > javascript – 如何简化if / else jquery片段

javascript – 如何简化if / else jquery片段

作者:互联网

我有一个超长的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/',
    });
});

解决方法:

这应该这样做:

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/',
    });
});

标签:simplify,javascript,jquery,jquery-plugins,minify
来源: https://codeday.me/bug/20190726/1544147.html