【发布时间】:2011-11-17 23:45:55
【问题描述】:
我正在尝试让我的主题将 CSS 类名称添加到 Drupal 7 主菜单中的“ul”元素。
HTML 模型代码:
<ul class="topnav">
<li><a href="#">Home</a></li>
<li>
<a href="#">Tutorials</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</ul>
如你所见,“第一层”ul的类名是“topnav”,下面嵌套的ul的类名是“subnav”。我正在连接一个 jQuery 下拉菜单,并希望在不使用模块的情况下应用它。是的,我尝试过“菜单属性”模块,但在这里不起作用。
我整天都在寻找,似乎找不到完整的解决方案。我知道超级鱼/和科幻附带的其他主题,但我想要自己的解决方案。
编辑: 好的,我已经走得更远了。我有一个菜单,其中 jquery 最初隐藏了 subs,但是,我无法让 jquery 在给定事件上显示 subs:将鼠标悬停在“ul.menu li span”上
这是我的 CSS:
ul.menu {
list-style: none;
padding: 0 20px;
margin: 0;
float: left;
width: 920px;
background: #222;
font-size: 1.2em;
background: url(topnav_bg.gif) repeat-x;
}
ul.menu li {
float: left;
margin: 0;
padding: 0 15px 0 0;
position: relative; /*--Declare X and Y axis base for sub navigation--*/
}
ul.menu li a{
padding: 10px 5px;
color: #000;
display: block;
text-decoration: none;
float: left;
}
ul.menu li a:hover{
background: url(topnav_hover.gif) no-repeat center top;
}
ul.menu li span { /*--Drop down trigger styles--*/
width: 17px;
height: 35px;
float: left;
background: url(../images/bullet.png) no-repeat center top;
}
ul.menu li span{background-position: center bottom; cursor: pointer;} /*--Hover effect for trigger--*/
ul.menu li ul {
list-style: none;
position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
left: 0; top: 35px;
background: #333;
margin: 0; padding: 0;
float: left;
width: 170px;
border: 1px solid #111;
}
ul.menu li ul.menu li{
margin: 0; padding: 0;
border-top: 1px solid #252525; /*--Create bevel effect--*/
border-bottom: 1px solid #444; /*--Create bevel effect--*/
clear: both;
width: 170px;
}
html ul.menu li ul.menu li a {
float: left;
width: 145px;
background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
padding-left: 20px;
}
html ul.menu li ul.menu li a:hover {
background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;
}
jQuery:
jQuery(function($) {
$('ul.menu li ul').hide();
$("ul.menu li ul").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
$("ul.menu li span").hover(function() { //When trigger is clicked...
// alert("No error!");
//Following events are applied to the subnav itself (moving subnav up and down)
$(this).parent().find("ul.menu li ul").slideDown('fast').show(); //Drop down the subnav on click
$(this).parent().hover(function() {
}, function(){
$(this).parent().find("ul.menu li ul").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
});
//Following events are applied to the trigger (Hover events for the trigger)
}).hover(function() {
$(this).addClass("subhover"); //On hover over, add class "subhover"
}, function(){ //On Hover Out
$(this).removeClass("subhover"); //On hover out, remove class "subhover"
});
});
如果我编辑这一行:$(this).parent().find("ul.menu li ul").slideDown('fast').show(); 到这个:("ul.menu li ul").slideDown('fast').show(); 它将显示所有父项的所有子菜单 - 显然我只想显示我悬停的父菜单的子菜单。当我尝试重新添加 $(this).parent()... 时,它根本不会显示任何潜艇。
谢谢。
【问题讨论】:
-
您可以使用 CSS 做到这一点:
ul#topnav > li > ul匹配原始ul内部的二级ul。 -
绝对按照 Blender 的建议,使用主题预处理功能改变它通常涉及字符串替换,这从来都不是很好
-
@Blender 不妨将其发布为答案。虽然我认为@Micheal 可能正在寻找
$('ul#topnav > li > ul');,所以他可以选择合适的元素。 -
感谢大家的回复。当我在 Chrome 中使用“检查元素”查看我的主菜单 CSS 时,我看到:
<div id="navigation"><div id="navigation"><ul class="links"><li class="menu-227 first active"><a href.... class = "active">Home</a>现在“主页”下应该是一个子菜单项,该子项的 ul 需要具有“subnav”类.所以使用@Blender 的技术,我基本上可以将“链接”类重命名为“topnav”? -
为了进一步澄清,这实际上是我试图用我的 Drupal 菜单完成的:noupe.com/tutorial/drop-down-menu-jquery-css.html
标签: css drupal-theming