【问题标题】:How to change hash /#/ for /#!/如何为 /#!/ 更改哈希 /#/
【发布时间】:2012-05-27 00:08:38
【问题描述】:

我正在使用 wordpress,我想在您单击任何本地链接后更改网址。我正在使用 ajax 加载页面,这就是我想要这样做的原因。
我可以更改 url,在“http://site.com/”和加载的内容 /example-page/ 之间添加一个哈希,结果:“http://site.com/#/example-page”,它加载了示例页面,但我想添加“!”标记以获取“http://site.com/#!/example-page”just like this theme
我正在使用jquery-hashchange 插件顺便说一句。
请让我知道您的想法。

这段代码是解决后编辑的,所以这是正确的代码。

jQuery(document).ready(function($) {
  var $mainContent = $("#container"),
    siteUrl = "http://" + top.location.host.toString(),
    url = '';
  $(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function() {
    location.hash = '!' + this.pathname;
    return false;
  });
  $("#searchform").submit(function(e) {
    location.hash = 's/' + $("#s").val();
    e.preventDefault();
  });
  $(window).bind('hashchange', function(){
    url = window.location.hash.substring(3);
    if (!url) {
      return;
    }
    url = url + " #content";
    $mainContent.animate({opacity: "0.1"}).html('Please wait...').load(url, function() {
      $mainContent.animate({opacity: "1"});
    });
  });
  $(window).trigger('hashchange');
});

【问题讨论】:

  • 欢迎来到该网站 - 我们标记已解决问题的方式是使用勾选图标标记已接受的答案,而不是编辑问题以表示已解决。

标签: jquery ajax wordpress hash


【解决方案1】:

改变

location.hash = this.pathname;

location.hash = "#!" + this.pathname;

(严格来说,您之前应该有哈希)并更改

url = window.location.hash.substring(1);

url = window.location.hash.substring(2);

还有变化

location.hash = '?s=' + $("#s").val();

location.hash = '#!?s=' + $("#s").val();

【讨论】:

  • 非常感谢尼尔。其实我找到了同样的答案呵呵。
  • location.hash = '?s=' + $("#s").val();对我不起作用:/
  • @CarlosSalas 我已经更新了我的答案,原来的版本有错误。
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 2015-01-20
  • 1970-01-01
相关资源
最近更新 更多