查看时突出显示子菜单项
我有一个菜单,基本上什么都没有,只是一堆彼此内部的无序列表。这是一个示例:
<div id="horiz-menu" class="moomenu"><div class="wrapper">
<ul class="menutop">
<li><a href="something.html"><span>Home Page (top menu item)</span></a>
<ul>
<li><a href="something2.html"><span>Second page (sub menu item)</span></a>
</li>
</ul>
</li>
<li class="parent active"><a href="something3.html"><span>Resources (top menu item)</span></a>
<ul>
<li id="current" class="active"><a href="something4.html"><span>Third Page (sub menu item)</span></a>
</li>
<li><a href="something5.html"><span>Fourth Page (sub menu item)</span></a>
</li>
</ul>
</li>
</ul>
</div></div>
当顶部菜单项悬停在上方时,它的样式看起来像顶部菜单项是水平导航菜单,子菜单项是垂直下拉列表(无滚动);以其他方式隐藏。这一切都有效。我的问题是,当访问者位于该页面上(而不是简单地悬停在该菜单项上)时,编码 css 的正确方法是什么,以便子菜单项突出显示(背景颜色应该改变)?
我已经尝试过:
#horiz-menu ul li.active {background-color:#000;}
#horiz-menu ul li#current {background-color:#000;}
我不知道这是否是这样做的方法。有人可以帮忙吗?如果您感到困惑,我希望能够使用 css 用 li class="active" 或 id="current" 为第二个 ul 着色。 “活动”和“当前”id 和类会动态应用于正在查看的页面的任何列表项。
谢谢!
I have a menu that is basically nothing but a bunch of unordered lists inside of each other. Here is an example:
<div id="horiz-menu" class="moomenu"><div class="wrapper">
<ul class="menutop">
<li><a href="something.html"><span>Home Page (top menu item)</span></a>
<ul>
<li><a href="something2.html"><span>Second page (sub menu item)</span></a>
</li>
</ul>
</li>
<li class="parent active"><a href="something3.html"><span>Resources (top menu item)</span></a>
<ul>
<li id="current" class="active"><a href="something4.html"><span>Third Page (sub menu item)</span></a>
</li>
<li><a href="something5.html"><span>Fourth Page (sub menu item)</span></a>
</li>
</ul>
</li>
</ul>
</div></div>
It is styled to look like the top menu items are the horiz nav menu and the sub menu items are a vertical drop down list (no scrolling) when the top menu item is hovered over; hidden other wise. That all works. My question is what would be the proper way to code css so the sub menu items get highlighted (the background color should change) when the visitor is on that page (not simply hovering over that menu item)?
I have tried this:
#horiz-menu ul li.active {background-color:#000;}
#horiz-menu ul li#current {background-color:#000;}
I don't know if this is the way to do this or not. Can some one help? If your confused I want to be able to use css to color the second ul with an li class="active" or an id="current". The "active" and "current" id and class get applied dynamically to what ever list item the page being viewed is.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 JQuery 中的 addClass 方法
Try the addClass Method in the JQuery
使用具有背景颜色的
.active
类应该可以工作,但是,这取决于您拥有的其他 CSS。您必须确保.active
或#current
的特异性大于默认样式 (#horiz-menu ul li
)。另外,请务必仔细检查是否具有默认背景 css 的li
而不是其中的锚点。Using the
.active
class with background color SHOULD work, however, it depends on what other CSS you have. You have to make sure the specificity of the.active
or#current
is greater than the default styling (#horiz-menu ul li
). Also, be sure to double check that it is theli
that has the default background css rather than the anchor inside of it.