有没有更好的方法在 jQuery 中实现下拉菜单?
我正在尝试使用 jQuery 的 hover
来执行下拉菜单。所以我尝试的第一件事是:
<ul id="menu">
<li>
<ul>
...
</ul>
</li>
...
</ul>
$(" #menu ul ").css({display: "none", visibility: "hidden"});
$(" #menu li ").hover(function() {
$(this).find('ul:first:hidden').css(visibility:"visible", display:"block"}).show('fast');
},
function() {
$(this).find('ul:first').css({visibility:"hidden", display:"none"});
});
这在 Firefox 中效果很好。不幸的是,在我运行的 Chrome 版本 (9.0.597.94) 中,如果你将鼠标悬停在下拉菜单上,它会在 $("#menu li#) 上触发鼠标。
好吧,我们有一个竞争条件,这是我所需要的要做的就是添加一些计时器...
(function(){
var timer;
$(" #menu li ").hover(function() {
$(this).find('ul:first:hidden').css(visibility:"visible", display:"block"}).show('fast');
},
function() {
var header = $(this);
timer = setTimeout(function() {
header.find('ul:first').css({visibility:"hidden", display:"none"});
}, 100);
});
$(" #menu li > ul ").hover(function() {
clearTimeout(timer);
},
function() {
$(this).css({visibility:"hidden", display:"none"});
});
}();
这确实很棒,直到有人说我们需要在下拉菜单中放置一个文本框
,当然,文本框会导致鼠标在 $( “ #menu li > ul ”),所以现在我必须放置另一个嵌套的计时器层,我对自己说:必须有更好的方法来做到这一点!
所以我希望有人能告诉我...
I'm trying to use jQuery's hover
to do drop down menues. So the first thing I try is:
<ul id="menu">
<li>
<ul>
...
</ul>
</li>
...
</ul>
$(" #menu ul ").css({display: "none", visibility: "hidden"});
$(" #menu li ").hover(function() {
$(this).find('ul:first:hidden').css(visibility:"visible", display:"block"}).show('fast');
},
function() {
$(this).find('ul:first').css({visibility:"hidden", display:"none"});
});
and this works great... in Firefox. Unfortunately in the version of Chrome I'm running (9.0.597.94) if you mouse over that drop down menu it fires the mouse out on $("#menu li#).
So ok, we have a race condition, all I need to do is add some timers...
(function(){
var timer;
$(" #menu li ").hover(function() {
$(this).find('ul:first:hidden').css(visibility:"visible", display:"block"}).show('fast');
},
function() {
var header = $(this);
timer = setTimeout(function() {
header.find('ul:first').css({visibility:"hidden", display:"none"});
}, 100);
});
$(" #menu li > ul ").hover(function() {
clearTimeout(timer);
},
function() {
$(this).css({visibility:"hidden", display:"none"});
});
}();
Which works real great, until someone says we need to put a textbox in that dropdown menu
; and of course a text box causes the mouse out to fire on $(" #menu li > ul "), so now I've got to put another nested layer of timers and I'm thinking to myself: There has got to be a better way to do this!
So I was hoping someone could clue me in...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否有理由不能使用 Suckerfish (或者变得复杂Superfish)?看来这是一个不需要重新发明的轮子......
Is there a reason you can't use Suckerfish (or to be sophistimacated Superfish)? It seems like this is a wheel that needn't be reinvented...
无论如何......我想你可以看看这里:38 个 jQuery 和 CSS 下拉多级菜单解决方案。
Anyway... I guess you can take a look here: 38 jQuery and CSS Drop Down Multi Level Menu Solutions.