jQuery - 活动链接和父关系
我正在开发一个网站的导航,需要一些有关动态地将类添加到活动链接的指导。此外,一旦建立了该链接,我需要引用回父级并让它“显示”。
这就是我正在做的工作。导航是手风琴样式,但不使用手风琴 UI。
<ul id="menu3">
<li><a href="{site_url}">Home</a></li>
<li><a class="drop" href="#">Information</a>
<ul>
<li><a href="{site_url}information/audio">Audio</a></li>
<li><a href="{site_url}information/video">Video</a></li>
<li><a href="{site_url}information/presentations">Presentations</a></li>
</ul>
</li>
<li><a class="drop" href="#">Clinics</a>
<ul>
<li><a href="{site_url}clinics/one">Office 1</a></li>
<li><a href="{site_url}clinics/two">Office 2</a></li>
</ul>
</li>
<li><a href="{site_url}forms/">Forms</a></li>
<li><a href="{site_url}success_stories/index">Success Stories</a></li>
然后我有一些 jQuery 来最初隐藏子菜单项:
$(function(){
$('ul#menu3 ul').hide();
$('ul#menu3 > li > a.drop').click(function(){
$(this).parent().children('ul').toggle("slow");
return false;
});
到目前为止一切顺利。一切正常。
现在我想动态突出显示活动链接,并尝试使用:
var path = location.pathname.substring(1);
if ( path )
$('ul#menu3 a[href$="' + path + '"]').attr('class', 'selected');
但它似乎不足以添加正确的类。
我需要做的最后一件事是向活动组打开导航。例如,如果音频是当前页面,则导航折叠面板的信息部分将打开以显示活动链接。
我真的很感激对此的一些帮助。谢谢。
I am working on a navigation for a site and need some guidance on dynamically adding a class to the active link. In addition, once that link is established and I need to reference back to the parent and have it "show".
This is what I am working with. The navigation is accordion style but does not use the Accordion UI.
<ul id="menu3">
<li><a href="{site_url}">Home</a></li>
<li><a class="drop" href="#">Information</a>
<ul>
<li><a href="{site_url}information/audio">Audio</a></li>
<li><a href="{site_url}information/video">Video</a></li>
<li><a href="{site_url}information/presentations">Presentations</a></li>
</ul>
</li>
<li><a class="drop" href="#">Clinics</a>
<ul>
<li><a href="{site_url}clinics/one">Office 1</a></li>
<li><a href="{site_url}clinics/two">Office 2</a></li>
</ul>
</li>
<li><a href="{site_url}forms/">Forms</a></li>
<li><a href="{site_url}success_stories/index">Success Stories</a></li>
Then I have a bit of jQuery to initially hide the submenu items:
$(function(){
$('ul#menu3 ul').hide();
$('ul#menu3 > li > a.drop').click(function(){
$(this).parent().children('ul').toggle("slow");
return false;
});
So far so good. Everything works.
Now I would like to dynamically highlight the active link and I tried using:
var path = location.pathname.substring(1);
if ( path )
$('ul#menu3 a[href$="' + path + '"]').attr('class', 'selected');
but it doesn't seem to be enough to add the correct class.
The last thing I need to do is to have the navigation open to the active group. For example if Audio is the current page, the Information section of the navigation accordion would be open to show the active link.
I would really appreciate some help on this. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容对我来说效果很好。您添加的“选定”代码也按预期工作。内嵌评论。如果问题仍然存在……评论/询问。
The following just works fine for me. Also your add "selected" code works as expected. Comments provided inline. If question remain open... comment/ask.