我的 JQuery 导航出了什么问题?
目前,我的网站有一个下拉菜单设置,如下所示:
HTML:
<ul class="primary-navigation">
<li><a href="/About" id="about"><span>About</span></a></li>
<li><a href="/Location" id="location"><span>Location</span></a></li>
<li><a href="/Site-Plan"><span>Site Plan</span></a></li>
<li><a href="/Specification"><span>Specification</span></a></li>
<li><a href="/Gallery"><span>Gallery</span></a></li>
<li><a href="/Investors"><span>Investors</span></a></li>
<li class="last"><a href="/Contact"><span>Contact</span></a></li>
</ul>
<ul class="secondary-navigation" id="about-menu">
<li><a href="/Item1">Item1</a></li>
<li><a href="/Item2">Item2</a></li>
</ul>
<ul class="secondary-navigation" id="location-menu">
<li><a href="/Item1">Amenities</a></li>
<li><a href="/Item2">Connections</a></li>
<li><a href="/Item3>Location Map</a></li>
</ul>
Javascript:
$(document).ready(function () {
// About
$("#about").hover(
function() {
$("#about-menu").css("display","block");
},
function() {
$("#about-menu").css("display","none");
}
);
$("#about-menu").hover(
function () {
$(this).css("display", "block");
},
function () {
$(this).css("display", "none");
}
);
$("#about-menu li").hover(
function () {
$("#about-menu").css("display", "block");
},
function () {
$("#about-menu").css("display", "none");
}
);
// Location
$("#location").hover(
function () {
$("#location-menu").css("display", "block");
},
function () {
$("#location-menu").css("display", "none");
}
);
$("#location-menu").hover(
function () {
$(this).css("display", "block");
},
function () {
$(this).css("display", "none");
}
);
});
CSS:
#header ul.primary-navigation { width: 735px; height: 90px; overflow: auto; position: absolute; top: 0px; right: 0px; background: #FFFFFF; }
#header ul.primary-navigation li { list-style-type: none; width: 104px; height: 89px; float: left; position: relative; border-right: 1px solid #FFFFFF; background: #b3b3b3; }
#header ul.primary-navigation li:hover { background: #adcc52; }
#header ul.primary-navigation li.last { border-right: 1px solid #b3b3b3; }
#header ul.primary-navigation li.last:hover { background: #adcc52; border-right: 1px solid #adcc52; }
#header ul.primary-navigation li a { display: block; width: 104px; height: 89px; text-decoration: none; }
#header ul.primary-navigation li a span { color: #FFFFFF; display: block; position: absolute; left: 10px; bottom: 10px; }
#header ul.secondary-navigation { width: 735px; display: none; position: absolute; top: 89px; left: 225px; background: red; overflow: auto; }
#header ul.secondary-navigation li { width: 735px; height: 30px; position: relative; float: left; /*background: #adcc52;*/ }
导航似乎首先工作。然而,似乎每当子菜单处于活动状态时,如果您尝试将鼠标悬停在第一个
项目之后的任何内容上,子导航就会消失。我一生都无法弄清楚为什么会发生这种事!您可能会认为,由于
中,因此它们都会显示,直到您移出 >
但它似乎不是这样工作的。有谁知道为什么会发生这种情况?
谢谢!
Currently I have a drop down menu setup for my website as follows:
HTML:
<ul class="primary-navigation">
<li><a href="/About" id="about"><span>About</span></a></li>
<li><a href="/Location" id="location"><span>Location</span></a></li>
<li><a href="/Site-Plan"><span>Site Plan</span></a></li>
<li><a href="/Specification"><span>Specification</span></a></li>
<li><a href="/Gallery"><span>Gallery</span></a></li>
<li><a href="/Investors"><span>Investors</span></a></li>
<li class="last"><a href="/Contact"><span>Contact</span></a></li>
</ul>
<ul class="secondary-navigation" id="about-menu">
<li><a href="/Item1">Item1</a></li>
<li><a href="/Item2">Item2</a></li>
</ul>
<ul class="secondary-navigation" id="location-menu">
<li><a href="/Item1">Amenities</a></li>
<li><a href="/Item2">Connections</a></li>
<li><a href="/Item3>Location Map</a></li>
</ul>
Javascript:
$(document).ready(function () {
// About
$("#about").hover(
function() {
$("#about-menu").css("display","block");
},
function() {
$("#about-menu").css("display","none");
}
);
$("#about-menu").hover(
function () {
$(this).css("display", "block");
},
function () {
$(this).css("display", "none");
}
);
$("#about-menu li").hover(
function () {
$("#about-menu").css("display", "block");
},
function () {
$("#about-menu").css("display", "none");
}
);
// Location
$("#location").hover(
function () {
$("#location-menu").css("display", "block");
},
function () {
$("#location-menu").css("display", "none");
}
);
$("#location-menu").hover(
function () {
$(this).css("display", "block");
},
function () {
$(this).css("display", "none");
}
);
});
CSS:
#header ul.primary-navigation { width: 735px; height: 90px; overflow: auto; position: absolute; top: 0px; right: 0px; background: #FFFFFF; }
#header ul.primary-navigation li { list-style-type: none; width: 104px; height: 89px; float: left; position: relative; border-right: 1px solid #FFFFFF; background: #b3b3b3; }
#header ul.primary-navigation li:hover { background: #adcc52; }
#header ul.primary-navigation li.last { border-right: 1px solid #b3b3b3; }
#header ul.primary-navigation li.last:hover { background: #adcc52; border-right: 1px solid #adcc52; }
#header ul.primary-navigation li a { display: block; width: 104px; height: 89px; text-decoration: none; }
#header ul.primary-navigation li a span { color: #FFFFFF; display: block; position: absolute; left: 10px; bottom: 10px; }
#header ul.secondary-navigation { width: 735px; display: none; position: absolute; top: 89px; left: 225px; background: red; overflow: auto; }
#header ul.secondary-navigation li { width: 735px; height: 30px; position: relative; float: left; /*background: #adcc52;*/ }
The navigation appears to be working at first. However it seems that whenever a submenu is active, if you try to hover over anything after the first <li>
item, the sub nav disappears. I can't for the life of me figure out why this is happening! You'd think that because the <li>
items are nested within a parent <ul>
, that they'd all be displaying until you move off the <ul>
but it doesn't seem to work that way.
Does anyone know why this is happening?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说的是,
当你离开 LI 时,第二个回调被调用,整个菜单消失。更多 LI 不会显示,也无法触发菜单再次显示。您需要删除此回调。
另一个技巧,不用
css("display...
),只需使用.show()
和.hide()
I would say that in
When you leave an LI, the second callback is called and the whole menu disappears. Further LIs are not shown and cannot trigger the menu to be shown again. You need to remove this callback.
Another trick, instead of
css("display...
, just use.show()
and.hide()