简体   繁体   English

单击列表项时关闭下拉列表

[英]Dismissing Drop Down List when List Item Clicked

I have a drop down list that is activated on hover (and dismissed on hover leave). 我有一个在悬停时激活的下拉列表(并在悬停假时被关闭)。 It works fine insofar as the hover is concerned. 就悬停而言,它工作正常。 When a user clicks one of the list items in the drop down list, I want the drop down list to be dismissed, just as if they have moved their mouse away from the list. 当用户单击下拉列表中的列表项之一时,我希望关闭下拉列表,就像他们已经将鼠标从列表中移开一样。 This is where I am stuck. 这就是我卡住的地方。

$(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();
    });
});

It may be of interest to note that the console output appears twice for each click. 可能需要注意的是,控制台输出每次单击都会出现两次。 Not sure what that suggests... 不知道这意味着什么...

You are targeting a class of ul , not a <ul> tag. 您定位的是ul ,而不是<ul>标签。

Change the last code to: 将最后一个代码更改为:

$('.navigation li').click(function () {
    console.log("clicked");
    $('ul', this).fadeOut(); // changed
});

It's often the simple things. 通常是简单的事情。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM