jQuery 手风琴菜单 - 保持手风琴菜单对我所在的页面打开
我希望你能帮忙。 我对 jQuery 很陌生,正在为我的侧面导航开发五级或六级手风琴菜单。到目前为止,我从 Dane Peterson @ daneomatic.com 那里获得了大部分代码(感谢 Dane!)。但是,我坚持一件事:
我想让我的手风琴/树像这样工作:
当我向下导航到第三级,然后单击链接打开链接到该级别的页面时,如何一旦第三级页面加载,我是否表明我在该页面上? 另外,当我加载页面时,如何使树保持打开到该级别?
我想我要问的是:有没有办法让手风琴/树自动更新以显示您所在的页面,并使树打开到该级别?
提前致谢!
I hope you can help.
I'm very new to jQuery and am working on a five- or six-level accordion menu for my side navigation. I got the majority of the code I have so far from Dane Peterson @ daneomatic.com (thanks Dane!). But, I'm stuck on one thing:
I'd like to have my accordion/tree work like this:
When I navigate down into, say, level three, and click on the link to open the page linked to that level, how do I indicate once the level three page loads that I'm on that page?
Also, how do I keep the tree open to that level when I load the page?
I guess what I'm asking is: is there a way for the accordion/tree to automatically update to show what page you're at, and have the tree open to that level?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要让手风琴根据 URL 自动打开正确的部分,您首先需要启用
navigation
选项,如下所示:默认情况下,此选项会查找具有 < 的手风琴标题链接。 code>href 与 URL 片段匹配(如果您的 URL 是 http://somesite.com/about# contact,#contact 是片段)并打开该标题链接的部分。由于您使用折叠面板导航到不同的页面,因此您可能没有要匹配的 URL 片段,因此您必须编写自定义
navigationFilter
:您可以使用
navigationFilter
选项覆盖 Accordion 插件如何将标题链接匹配到当前页面的 URL。到目前为止,我们已经根据当前页面打开了手风琴的正确部分。接下来,我们需要突出显示与该页面对应的部分中的链接。您可以使用以下方法来完成此操作:
在这里,我们将浏览导航手风琴中的所有页面链接,选择与当前 URL 匹配的链接,然后对其应用
.active
类,这然后你可以使用 CSS 设计不同的样式。旁白:完成第二部分的另一种可能更好的方法是使用已应用于适当链接的
.active
类来构建页面,但这假设您已经控制后端并且您知道如何做到这一点。事实上,如果是这种情况,您可以跳过整个navigationFilter
事情并生成一个块来设置
active
选项手风琴打开右侧部分。To get the accordion to automatically open the correct section based on the URL, you'll start with enabling the
navigation
option with something like:By default, this option looks for the accordion header link that has an
href
that matches the URL fragment (if your URL is http://somesite.com/about#contact, #contact is the fragment) and opens that header link's section. Since you're using the accordion to navigate to different pages, you probably won't have URL fragments to match against, so you'll have to write a customnavigationFilter
:You can use the
navigationFilter
option to override how the accordion plugin matches header links to the URL of the current page.So far, we've got the right section of the accordion to open based on the current page. Next, we need to highlight the link in that section that corresponds to the page. You'll do that with something like:
Here we're going through all the page links in the navigation accordion, picking the one that matches the current URL, and applying an
.active
class to it, which you can then style differently with CSS.An aside: another, probably better, way to accomplish the second part is to build the page with the
.active
class already applied to the appropriate link, but that assumes you have control over the backend and that you know how to do it. In fact, if that's the case, you could skip the wholenavigationFilter
thing and generate a<script>
block to set theactive
option on the accordion to open the right section.好吧,这个问题已经困扰我一段时间了,我想我会发布我的解决方案来解决这个问题。 (我是 JQuery 的新手,所以......)
在我的情况下,我有一个母版页,其中包含用于处理菜单自动化的 JQuery 脚本,并且我有几个具有不同菜单的内容页面(我有一个可以说,水平菜单横跨页面标题,然后 JQuery 手风琴处理子菜单)。
我将 id 标签添加到菜单标题 div,然后将以下内容放置在内容页面的内容占位符中。
这非常有效,它确实让我想知道为什么我在过去一周左右的时间里一直在努力解决这个问题,而解决方案是如此简单!
OK this issue has been bugging me for a while and thought I would post my solution to this problem. (I am a bit of a newbie with JQuery so....)
In my circumstance I have a master page that contains the JQuery script to handle the automation of the menu and I have several content pages that have different menus (I have a horizontal menu accross the page header and then the JQuery accordion handles the sub menu so to speak).
I added id tags to the menu header divs and then placed the following in the content placeholder of the content page.
This works perfectly and it did make me wonder why I have struggled with this over the last week or so when the solution is so simple!
这是 JavaScript 导航网站的陷阱之一 - 您的 URL 实际上并不像传统页面一样指向您的页面。这使得使用书签和后退按钮等普通浏览器功能变得困难。
如今,一些人似乎使用的一种解决方案是将这些信息存储在 url 的哈希部分之后。
通过存储信息来代替上面的“jsPageIndicator”,您可以在 $(document).ready() 之后使用 JavaScript 对其进行解析,并让它告诉您应该加载哪个页面。在您的情况下,这可能很简单,例如具有焦点的手风琴的索引(应该打开)。
您可能还想查看 jQuery 历史插件。
或者,正如 Alex 在下面指出的,benalman.com/projects/jquery-bbq-plugin
This is one of the pitfalls of JavaScript navigated websites - your URL doesn't actually point to your page, like a traditional page. It makes it difficult to use normal browser features like bookmarks and the back button.
One solution some people seem to be using these days is to store this information after the hash part of the url.
By storing information in place of "jsPageIndicator" above, you can then parse it with the JavaScript after $(document).ready(), and have it tell you what page should be loaded. In your case this might be something simple, such as the index of the accordion that has the focus (should be open).
You might also want to look at the jQuery history plugin.
Or, as Alex points out below, benalman.com/projects/jquery-bbq-plugin
以上都不适合我。 jquery ui 的文档是多余的,并且源代码没有给不熟悉 jquery 的人留下太多线索。
我在侧边栏中使用了手风琴,每个内容部分中的链接都在表格内,所以我必须跟踪我的 HTML 结构(一个脆弱的东西),并在创建手风琴后立即执行此操作:
是的,可怕的,备份 tr、tbody、table 和 div,但它有效,在我们的例子中,我们已经几个月没有更改 HTML 结构了。
None of the above worked for me. The documentation for jquery ui is spare, and the source code didn't leave many clues for somebody not well versed in jquery.
I used the accordion in a sidebar, and the links in each content section were inside tables, so I had to keep track of my HTML-structure (a fragile thing), and do this right after creation of the accordion:
Yes, horrific, backing up a tr, tbody, table, and div, but it worked, and in our case we haven't changed the HTML-structure in months.