我有一个在悬停时激活的下拉列表(并在悬停离开时关闭)。就悬停而言,它工作正常。当用户单击下拉列表中的列表项之一时,我希望关闭下拉列表,就像他们将鼠标从列表中移开一样。这就是我被困的地方。
$(document).ready(function () {
$('.navigation li').hover(
function () {
$('ul', this).fadeIn();
},
function () {
$('ul', this).fadeOut();
}
);
// this is where I am attempting to dismiss the drop down list
// the console output appears just fine, but the drop down list does not
// go away.
$('.navigation li').click(function () {
console.log("clicked");
$('.ul', this).fadeOut();
});
});
您可能会注意到,每次单击都会出现两次控制台输出。不确定这意味着什么......
最佳答案
您的目标是 ul
的类 , 不是 <ul>
标签。
将最后的代码改为:
$('.navigation li').click(function () {
console.log("clicked");
$('ul', this).fadeOut(); // changed
});
这通常是简单的事情。 :-)
关于javascript - 单击列表项时关闭下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28883399/