简体   繁体   English

隐藏父ul时如何在li上使用jquery show()

[英]how to use jquery show() on li when parent ul is hidden

In one of my function I am hiding the parent unordered list at the beginning of page load and based on certain condition I am showing one of the list element under the hidden parent ul. 在我的功能之一中,我将在页面加载开始时隐藏父级无序列表,并基于某些条件,在隐藏的父级ul下显示list元素之一。 However, the below code is not working. 但是,以下代码不起作用。 Using below mentioned function <ul id="menu"> is hidden completely and I am not able to populate the respective li elements. 使用下面提到的函数<ul id="menu">完全被隐藏了,我无法填充相应的li元素。

HTML mark up HTML标记

<ul id="menu">
    <li id="mainMenuGroup_mostPopular"></li>
    <li id="mainMenuGroup_slots"></li>
    <li id="mainMenuGroup_table"></li>
    <li id="mainMenuGroup_roulette"></li>
    <li id="mainMenuGroup_poker"></li>
    <li id="mainMenuGroup_mobileiOS"></li>
    <li id="mainMenuGroup_mobileAndroid"></li>
    <li id="mainMenuGroup_mobileWinPhone"></li>
</ul>

This is my function 这是我的职责

function deviceDetectionScript() {

    var mobileDetection = new MobileDetect(window.navigator.userAgent);

    if (mobileDetection.mobile()) {

        $('ul#menu').hide();

        if (mobileDetection.os() == 'iOS') {
            $('ul#menu').find('li#mainMenuGroup_mobileiOS').show();
            show_tab_content('mobileiOS');
        }

        if (mobileDetection.os() == 'AndroidOS') {
            $('ul#menu').find('li#mainMenuGroup_mobileAndroid').show();
            show_tab_content('mobileAndroid');
        }

        if (mobileDetection.os() == 'WindowsMobileOS' || mobileDetection.os() == 'WindowsPhoneOS') {
            $('ul#menu').find('li#mainMenuGroup_mobileWinPhone').show();
            show_tab_content('mobileWinPhone');
        }

    } else {
        $('ul#menu').show();
    }
}

First of all, you can not show child elements if the parent element is hidden. 首先,如果父元素被隐藏,则无法显示子元素。 You need to hide all the li s except the specific one. 您需要隐藏除特定li之外的所有li You can use .not() method for that. 您可以使用.not()方法。

 if (mobileDetection.mobile()) {
     if (mobileDetection.os() == 'iOS') {
         $('ul#menu').find("li").not('#mainMenuGroup_mobileiOS').hide();
         show_tab_content('mobileiOS');
     }

you don't need to hide ul. 您不需要隐藏ul。 you should hide li except you want to show li. 除了要显示李,您应该隐藏李。 like this 像这样

$('#menu > li').not('#mainMenuGroup_table').hide();

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

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