我正在使用 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/