如何用另一个响应替换窗口的 URL 哈希? [英] How can I replace a window's URL hash with another response?

查看:19
本文介绍了如何用另一个响应替换窗口的 URL 哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用替换方法更改散列 URL (document.location.hash),但它不起作用.

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.
  });

});

我不想更改/刷新页面,我只想替换 URL 而没有任何响应.

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

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

Note: I don't want to solve this with a href="#food" solution.

推荐答案

使用 locationwindow.location 而不是 document.location因为后者是非标准的.

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

window.location.hash = '#food';

这会将 URL 的哈希替换为您为其设置的值.

This will replace the URL's hash with the value you set for it.

参考

这篇关于如何用另一个响应替换窗口的 URL 哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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