【问题标题】:Current URL hashchange event append new hash to target URL and link out to it当前 URL hashchange 事件将新哈希附加到目标 URL 并链接到它
【发布时间】:2019-06-22 23:40:10
【问题描述】:

我有一个页面 ex: test123.com/go#1 如果哈希发生变化,那么该页面上 ex: test123.com/go#2 我想将新的哈希值附加到目标 URL 并链接到它:test123.com/gopage2#2

这是我迄今为止尝试过的代码:

var url = window.location.href;
var res = url.split("#");
var hash = "#" + res[1];
$(window).on('hashchange', function() {
  window.location = 'test123.com/gopage2' + hash;
});

当哈希值发生变化时,什么都没有发生......有什么想法吗?

【问题讨论】:

    标签: javascript jquery hash window.location hashchange


    【解决方案1】:

    假设您打算在事件处理程序中更新 url、res 和 hash,并在新 URL 中使用哈希,试试这个:

    $(window).on('hashchange', function() {
      var url = window.location.href;
      var res = url.split("#");
      var hash = "#" + res[1];
      window.location = 'test123.com/gopage' + res[1] + hash;
    });
    

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 2019-06-22
      • 2013-06-07
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多