3

適切なロジックで単純化できると思われる非常に長いjqueryスニペットがあります。やっと動くようになったので触るのが怖いです。

「else $target = #none」は文字通り「何も表示しない」ステートメントです。それをもっと上手に表現する方法がわかりませんでした。

本当にありがとう!-ジーム

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

4 に答える 4

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');
        }
于 2011-02-08T00:42:26.290 に答える
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/',
    });
});
于 2011-02-08T00:55:57.643 に答える
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コード中に変更されないため、さらに低下する可能性がありますが、プラグインを 3 回呼び出す必要があるかどうかはわかりません。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);
于 2011-02-08T00:43:35.077 に答える
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/'
            });

        }
    });
});
于 2011-02-08T00:49:50.610 に答える