1

我正在开发一个使用同位素过滤器和哈希历史的网站。
但是,为了让我的过滤器工作,我需要#filter=.print在我的缩略图的永久链接之后添加"a.perma"。这".print"是被点击的过滤器的类"option-set a"。在这种情况下,打印过滤器。
我对 jQuery 不太熟练,任何帮助将不胜感激。
这是我一直在弄乱的代码:

var myClass;

jQuery("option-set a").click(function() {
    myClass = jQuery(this).attr("class");
});

jQuery("a.perma").each(function() {
    var _href = jQuery(this).attr("href"); 
    jQuery(this).attr("href", _href + myClass);
});
4

2 回答 2

1

这是工作代码。删除任何以前的过滤器。即使在转到另一个页面并返回后仍保留永久链接值。用途localStorage.

if(typeof(Storage)!=="undefined") {
    var oldClass = localStorage.getItem("permalink");

    if(oldClass != null && oldClass != "") {
        jQuery("a.perma").each(function() {
            var _href = jQuery(this).attr("href"); 
            _href = _href.split("#")[0];

            jQuery(this).attr("href", _href + "#filter=." + oldClass);
        });
    }
}

jQuery("option-set a").click(function() {
    myClass = jQuery(this).attr("class");

    jQuery("a.perma").each(function() {
        var _href = jQuery(this).attr("href"); 
        _href = _href.split("#")[0];

        jQuery(this).attr("href", _href + "#filter=." + myClass);

        if(typeof(Storage)!=="undefined") {
            localStorage.setItem("permalink", myClass);
        }
    });
});
于 2013-01-07T14:50:23.933 回答
1

You could try this, using .attr() with a function to determine and set the new value to avoid having to iterate and get/set separately.

jQuery("option-set a").click(function() {
    var myClass = this.className; // same as $(this).attr('class');
    jQuery('a.perma').attr('href', function(i, oldHref) {
        return oldHref + '.' + myClass;
    });
});

Depending on what the original href attributes look like you may have to add more or less to the string; I've assumed in the above that the #filter= part is already included. You may also want to check that it's not already in the existing href so it's not added more than once.

于 2013-01-07T14:52:39.560 回答