使用JavaScript检测URL哈希值的变化 [英] Detect change in hash value of URL using JavaScript
问题描述
我正在开发一个简单的应用程序,该应用程序基于页面(由于项目限制)而具有动态内容.我了解动态内容,但是我不了解的是如何设置一个脚本,当URL中的哈希值更改时,该脚本会更改div
的html.
I'm working on a simple application which is single page based (due to project restrictions) and has dynamic content. I understand the dynamic content alright but what I don't understand is how to set-up a script that changes the html of a div
when the hash value in the URL changes.
我需要一个JavaScript脚本才能这样工作:
I need a JavaScript script to work as such:
网址:http://foo.com/foo.html
div内容:<h1>Hello World</h1>
Url: http://foo.com/foo.html
div contents: <h1>Hello World</h1>
网址:http://foo.com/foo.html#foo
div内容:<h1>Foo</h1>
Url: http://foo.com/foo.html#foo
div contents: <h1>Foo</h1>
这将如何工作?
请帮助!谢谢.
推荐答案
您可以收听hashchange
事件:
$(window).on('hashchange',function(){
$('h1').text(location.hash.slice(1));
});
这篇关于使用JavaScript检测URL哈希值的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!