jQuery - 在同一窗口中启动 URL,然后刷新
我正在使用 jQuery-UI 的手风琴插件和哈希标签导航,但我遇到了问题。我网站的每个页面上都有一个手风琴小部件。我的主导航菜单第二级中的 URL 应该使用随附的哈希标签打开手风琴的正确面板,它们的写法如下:
<li class="lvl2"><a href="/thepage.jsp#panel-3">panel 3</a></li>
问题是,如果您已经在查看以下任一面板中的一个面板,手风琴,试图用第二级导航打开不同的面板是行不通的。它将哈希标签传递到地址栏中,但除非您刷新页面,否则它不会打开折叠面板。所以我想我可以通过添加一些 jQuery 来解决这个问题,这会导致页面在 500 毫秒后刷新,如下所示:
$('.lvl2 a').click(function() {
setTimeout(function() {
location.reload();
},500);
});
除了现在破坏从页面到新页面的导航远离并且 一个新的手风琴面板。这告诉我答案可能是完全控制二级导航的所有功能并使用 jQuery 处理它。
那么我该如何更改它以“存储”锚标记的 href 属性中的 URL,将其传递到浏览器,然后刷新页面呢?
I'm using jQuery-UI's accordion plugin with hash tag navigation, but I'm running into a problem. Each page of my site has an accordion widget on it. The URLs in the second level of my main navigation menu are supposed to open the correct panel of the accordion using the accompanying hash tag, and they are written like so:
<li class="lvl2"><a href="/thepage.jsp#panel-3">panel 3</a></li>
The problem is, if you're already looking at one panel of any of the accordions, trying to make a different panel open up with the second level nav doesn't work. It passes the hash tag into the address bar, but it doesn't open the accordion panel unless you refresh the page. So I figured I could solve the problem by adding some jQuery that would cause the page to refresh after 500 milliseconds, like so:
$('.lvl2 a').click(function() {
setTimeout(function() {
location.reload();
},500);
});
Except now that sabotages navigation away from the page to a new page and a new accordion panel. Which tells me the answer may be to take full control of the all functionality of the second level navigation and handle it with jQuery.
So how could I alter this to "store" the URL from the href attribute of the anchor tag, pass it to the browser, and then refresh the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
lvl2
手风琴是动态的,您可能需要使用live
或liveQuery
而不是click
。If
lvl2
accordians are dynamic, you may need to uselive
orliveQuery
instead ofclick
.我又对它进行了一些尝试,并设法让它以一种似乎一致的方式工作。基本上,我将所有 lvl2 锚标记中的 href 属性更改为 rel 属性,然后在 jQuery 函数中使用该属性来处理新 URL 的导航。然后我将超时从 500 毫秒增加到 1000 毫秒。任何少的内容似乎都会在页面有机会访问新 URL 之前刷新页面太快。
I played around with it some more and managed to get it to work in what seems to be a consistent manner. Basically, I changed the href attributes in all of the lvl2 anchor tags to rel attributes and then used that attribute in my jQuery function to handle the navigation to the new URL. Then I increased the timeout from 500 to 1000 milliseconds. Anything less seems to refresh the page too soon, before it gets a chance to go to the new URL.