jquery - 如何更改 hash/#/for/#!/

标签 jquery ajax wordpress hash

我正在使用 wordpress,我想在您单击任何本地链接后更改 url。我正在使用ajax加载页面,这就是我想这样做的原因。我可以通过在“http://site.com/”和加载的内容/example-page/之间添加哈希来更改url ,结果:“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');
});

最佳答案

改变

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();

关于jquery - 如何更改 hash/#/for/#!/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10669198/

相关文章:

jQuery - 在 textarea 更改时,发布到 php 脚本并使用结果更新 div

JQuery Mobile native 选择不适用于机器人

jquery - 删除div onclick(使用remove)

ajax - 如何在 Django 中创建等待页面

javascript - 从 Dojo 转换为 jQuery

ajax - 通过 CORS 请求设置 cookie

php - 通过ajax/json发送两个变量

javascript - WordPress JavaScript 错误

wordpress - 隐藏运输选项 Woocommerce

mysql - 一个 MySQL 查询中的两个独立请求