通过URL中的hash标记在页面加载时执行AJAX调用

do AJAX call on page load through hash tag in URL?

本文关键字:加载 执行 调用 AJAX 中的 URL hash 通过      更新时间:2023-09-26

我使用同位素,这里是jsfiddle:http://jsfiddle.net/desandro/DA3wF/

或者,我设置了一个演示:http://nerdi.net/isotope-test.html

有一个选项filter: selector

我如何将一个过滤器(例如:index.html#green)传递给.green类?这可能吗?

filter: (window.location.hash != '') ? '.' + window.location.hash.replace('#', '') : 'default'

这是一个if/then语句,用于检查当前哈希是否为空字符串。如果是,则filter将被设置为default,否则将被设置成window.location.hash值(减去#)。

像这样的URL index.html将导致filter被设置为default,而像这样的网址index.html#my-selector将导致filter被设置为.my-selector

更新

你的代码不工作的原因实际上与我的代码无关,但这里是我在你的网站上测试的你的代码的更新版本:

$filterLinks.click(function(){
    var $this = $(this);
    
    // don't proceed if already selected
    if ( $this.hasClass('selected') ) {
        return;
    }
    
    $filterLinks.filter('.selected').removeClass('selected');
    $this.addClass('selected');
    
    // get selector from data-filter attribute
    var selector = $this.data('option-value');
    
    $container'.isotope({
        filter: selector
    });
});
filter: window.location.hash.replace('#', '.')

location.hash将返回"#green",我们只需将其替换为一个点即可获得".green"。

jsfiddle提供了如何根据散列更改所选链接的示例。