clicking on dropdown content, dropdown getting hide

前端 未结 1 549
萌比男神i
萌比男神i 2021-01-22 19:56

HTML code for dropdowns:

  • Locality
相关标签:
1条回答
  • 2021-01-22 20:03

    Updated with exact solution: The advice of super specific DOM element targeting still stands. jsFiddle Example

    var $dropShow = function showList() {
        $('.dropDownLink').on('click', function () {
            $('.dropDown').hide();
            $(this).children($('.dropDown')).show();
        });
    };
    var $navSelections = $('li.dropDown-row input, li.dropDown-row, ul.dropDown li.dropDown-row input, li.dropDownLink, ul li.dropDownLink');
    
    var $bodyClick = function hideAll() {
        $(this).on('click', function (e) {
            if (!$navSelections.is(e.target)) {
                $('.dropDown').slideUp(150);
            }
        });
    };
    
    $('.dropDownLink').not($navSelections).click($dropShow());
    $('*').not($navSelections).click($bodyClick());
    

    Pinpoint the pathing that you want the action to occur on only. If it is only supposed to occur, for example on a div, then use $('div.dropDownLink'). If it is only links in say, a navMenu that you want this functionality applied to, then you use $('navMenu div.dropDownLink'). Essentially, the more accurate your selector the better the results will be.

    If however, you would like to take the not so accurate approach you can use the .not() method to leave out very specific things.

    var $parentOfDropDown = $('div#navMenu');
    $('.dropDown').not($parentOfDropDown).hide();
    
    0 讨论(0)
提交回复
热议问题