0

重複の可能性:
URL に既にクリックしたリンクの href が含まれているかどうかを確認する

私の URL のいくつかは、次のようになります。

mydomain.com/t-shirts+white+white

それをフィルタリングする方法はありますか?

Jクエリ:

$('#coll-filter li a').one('click', function () {
  jQuery(this).attr("href", window.location.href  + '+' +$(this).attr('href'));
    jQuery('#coll-filter li a').each(function() { 
       if (window.location.href.indexOf($(this).attr('href')) != -1) {
             alert("no")
         }

    });

 });
4

1 に答える 1

1

これにより、重複したフィルターが削除されます。

function removeDupFilters(str) {
    var pos = str.search(/\/.*?$/), path, items, map = {}, i;
    if (pos !== -1) {
        path = str.substr(pos + 1);
        items = path.split("+");
        for (i = 0; i < items.length; i++) {
            map[items[i]] = true;
        }
        items = [];
        for (i in map) {
            items.push(i);
        }
        return str.substr(0, pos + 1) + items.join("+");
    }
    return str;
}

実際のデモ: http://jsfiddle.net/jfriend00/ntb8f/

コードで使用すると、次のようになります。

$('#coll-filter li a').one('click', function (e) {
    var url = removeDupFilters(window.location.href + '+' + $(this).attr('href'));
    if (url !== window.location.href) {
        // go to the new URL
        window.location.href = url;
    }
    e.preventDefault();
});

または、次のようにその関数を使用せずに、現在の URL を直接確認することもできます。

$('#coll-filter li a').one('click', function (e) {
    var filter = $(this).attr('href');
    var re = new RegExp("/|\\+" + filter + "$|\\+", "i");
    if (!re.test(window.location.href)) {
        // go to the new URL
        window.location.href = window.location.href + "+" + filter;
    }
    e.preventDefault();
});
于 2012-10-15T21:48:42.817 に答える