AJAX 是否通过 URL 中的哈希标签调用页面加载?

发布于 2024-12-29 21:45:55 字数 385 浏览 0 评论 0原文

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

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

有一个选项 filter: 选择器

我如何传递过滤器,例如: index.html html#green 在页面加载时会过滤到 .green 类吗?这可能吗?

I am using isotope, here's the jsfiddle: http://jsfiddle.net/desandro/DA3wF/

Or, alternatively, a demo I have set up: http://nerdi.net/isotope-test.html

There's an option filter: selector

How could I pass a filter, ex: index.html#green that on page load, would filter to the .green class? is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2025-01-05 21:45:55
filter: (window.location.hash != '') ? '.' + window.location.hash.replace('#', '') : 'default'

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

像这样的 URL,index.html,将导致 filter 设置为 default 以及像这样的 URL,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 != '') ? '.' + window.location.hash.replace('#', '') : 'default'

This is an if/then statement that checks to see if the current hash is an empty string. If it is then filter will be set to default otherwise it will be set to the window.location.hash value (minus the #).

A URL like this, index.html, will result in filter being set to default and a URL like this, index.html#my-selector, will result in filter being set to .my-selector.

Update

The reason your code isn't working doesn't actually have anything to do with my code, but here is an updated version of your code that I tested on your site:

$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
    });
});
谜兔 2025-01-05 21:45:55
filter: window.location.hash.replace('#', '.')

location.hash将返回“#green”,我们只需将其替换为点即可得到“.green”。

jsfiddle 包含如何根据哈希更改所选链接的示例。

filter: window.location.hash.replace('#', '.')

The location.hash will return "#green", we just replace it with a dot to get ".green".

jsfiddle with example of how to also change the selected link based on the hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文