I am trying to change a hashed URL (document.location.hash) with the replace method, but it doesn't work.

$(function(){
 var anchor = document.location.hash;
 //this returns me a string value like '#categories'

  $('span').click(function(){

     $(window).attr('url').replace(anchor,'#food');
     //try to change current url.hash '#categories'
     //with another string, I stucked here.
  });

});

I dont want to change/refresh page, I just want to replace URL without any responses.

注意:我不想用href="#Food"解决方案来解决这个问题.

推荐答案

Either use location or window.location instead of document.location as the latter is a non-standard.

window.location.hash = '#food';

这将用您为其设置的值替换URL的散列.

Reference

Jquery相关问答推荐

退出 jquery.each 循环后

如何在jQuery中移动表格行?

如何使用 jQuery 将分钟转换为小时/分钟并添加各种时间值?

根据属性'data-sort'对jQuery中的div进行排序?

如何在 HTML选项标签上显示工具提示?

如何使用 Twitter Bootstrap 弹出框进行 jQuery 验证通知?

聚焦 时防止 iphone 默认键盘

如何使所有浏览器都支持 ?有什么 Select 吗?

将字符串true和false转为布尔值

使用 Jquery 更改页面标题

如何使用 jQuery 隐藏 div?

如何通过jQuery函数仅获取直接子元素

5秒后jQuery自动隐藏元素

如果一个元素正在被动画,我如何用 jQuery 找出?

如何使用 jQuery 检测 IE 8?

使用 jQuery 在 Select 列表中隐藏选项

如何复制 pinterest.com 的绝对 div 堆叠布局

D3 和​​ jQuery 有什么区别?

Ajax 处理中的无效 JSON 原语

jQuery .val 更改不会更改输入值