在jQuery中更改散列而不重新加载 [英] Change hash without reload in jQuery

查看:99
本文介绍了在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哈希标记发生变化当你点击时,人们可以复制并书签。目前,当哈希标签发生变化时,这有效地重新加载页面。



是否可以更改哈希标记并且不重新加载页面以防止跳跃效果? / p>

解决方案

这适用于我

 <$ c $ (); 
($。'tab')。hide();
window.location.hash = this.hash;
$($(this).attr('href'))。fadeIn('slow');
});

请点击 http://jsbin.com/edicu 获得几乎相同代码的演示


I have the following code:

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

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.

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

解决方案

This works for me

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

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

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

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