基于 jQuery 锚点的导航
我正在寻找一种在页面之间导航并在锚点随 jQuery 发生变化时响应事件的方法。为了说明我的问题,让我用一个例子来演示:
- 用户进入一个已加载 jQuery 的页面,没有 jQuery UI 或任何其他插件。
- 页面加载时,用户会从
http://thesite.com/
重定向到http://thesite.com/#/
,无需任何页面刷新。 - 单击如下链接:
我的简历
。 - jQuery 使用
$.ajax()
方法加载请求的页面,无需刷新页面。 - 用户单击浏览器后退按钮,然后转到
http://thesite.com/#/
,无需刷新页面。
我擅长加载页面内容。但是,这是我不知道的:
- 如果页面加载时未提供,如何重定向到
http://thesite.com/#/
? - 单击带有锚分隔线 (#) 的链接时,如何监听加载页面而不刷新的请求?
- 最重要的是,我如何侦听浏览器后退/前进请求,知道要加载哪个页面?
抱歉,问题列表很崩溃。如果我的场景还不够清晰,那么这里有一个很棒的网站已经做到了:http://grooveshark.com/。
谢谢您的宝贵时间!我将慷慨地奖励彻底的答案/评论。
I am looking for a way to navigate between pages and respond to events when the anchor changes with jQuery. In order to illustrate my question, let me demonstrate with an example:
- A user enters a page which has loaded jQuery, without the jQuery UI or any additional plugins.
- As the page loads, the user is redirected from
http://thesite.com/
tohttp://thesite.com/#/
, without any page refresh. - A link, like this, is clicked:
<a href="#/author/bio">My Bio</a>
. - jQuery uses the
$.ajax()
method to load the requested page, without a page refresh. - The user clicks the browser back button, and goes to
http://thesite.com/#/
, without a page refresh.
I am good when it comes to loading the page content. However, here is what I don't know:
- How do I redirect to
http://thesite.com/#/
, if it is not supplied when the page loads? - How do I listen for a request to load a page without a refresh, when a link with an anchor divider (#) is clicked?
- Most importantly, how do I listen for browser back/forward requests, the know what page to load?
Sorry for the crash list of questions. If my scenario wasn't clear enough, then here is a great site that already does it: http://grooveshark.com/.
Thank you for your time! I will reward generously for thorough answers/comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您必须向 a 标签添加 onclick 。
那么你所要做的就是:
要监听哈希更改,你需要一个像 这样的插件
Well you would have to add an onclick to the a tags.
Then all you have to do is:
And to listen in on hash changes you need a plugin like this one
首先加载jquery.
准备好后更改所有网址,例如
First load jquery.
on ready change all urls something like