在 jQuery 中无需重新加载即可更改哈希 [英] Change hash without reload in jQuery

查看:39
本文介绍了在 jQuery 中无需重新加载即可更改哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$('ul.questions li a').click(function(event) {
    $('.tab').hide();
    $($(this).attr('href')).fadeIn('slow');
    event.preventDefault();
    window.location.hash = $(this).attr('href');
});

这只是根据您点击的时间淡入一个 div,但我希望页面 URL 哈希标签在您点击时更改,以便人们可以复制它并将其添加为书签.目前,当哈希标签更改时,这会有效地重新加载页面.

This simply fades a div in based on when you click but I want the page URL hash tag to change when you click so people can copy and bookmark it. At the moment this effectively reloads the page when the hash tag is change.

是否可以更改hash标签而不重新加载页面以防止跳转效果?

Is it possible to change the hash tag and not reload the page to prevent the jumping effect?

推荐答案

这对我有用

$('ul.questions li a').click(function(event) {
    event.preventDefault();
    $('.tab').hide();
    window.location.hash = this.hash;
    $($(this).attr('href')).fadeIn('slow');
});

在这里http://jsbin.com/edicu查看代码几乎相同的演示

Check here http://jsbin.com/edicu for a demo with almost identical code

这篇关于在 jQuery 中无需重新加载即可更改哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆