jquery mobile menu to close after i click a menu i

2019-05-04 15:11发布

问题:

based on this demo: http://webdesignerwall.com/demo/mobile-nav/ this is my code.

<script type="text/javascript">
    jQuery(document).ready(function($){

        /* prepend menu icon */
        $('#nav-wrap').prepend('<div id="menu-icon"><img id="logo" src="<?php echo site_url(); ?>/wp-content/themes/blue-and-grey/images/mobileimages/hme_btn.png" /></div>');

        /* toggle nav */
        $("#menu-icon").on("click", function(){
            $("#nav").slideToggle();
            $(this).toggleClass("active");
        });

    });
    </script>

what i need is the menu to "close" after i click a menu item because it's a single page website, and i don't want it to stay opened after i clicked. how do i do that?

thank you so much!

回答1:

Add an extra event to make it happen. You could trigger the click of #menu-icon when some menu item is clicked:

 $("#nav").on("click", "li", function () {
       $("#menu-icon").click();
       //or $("#nav").slideToggle();
 });

Demo : http://jsfiddle.net/hungerpain/RtMNj/2/



回答2:

Just in case someone will stumble upon this question where you are using Primefaces Mobile along with jQuery Mobile, here is how I was able to make a workaround since the accepted solution here didn't work out for me.

Basically I just put an "invisible" a tag that will be the one to trigger the closing of the pop up. When user clicks on the other menu item it will execute a javascript click() command directed to that "invisible" a tag.

<ul data-role="listview" data-theme="c" data-divider-theme="a">
                    <li data-role="list-divider">Menu</li> 
                    <li data-icon="user">
                        <p:commandLink id="show-member-menu" styleClass="bordercolorddd"
                            value="Members" data-rel="back"  onclick="$('#closer').click();"
                            action="#{editROrgUnit.initializeSubordinateList}" 
                            update=":main:subordlist" process=":main:subordlist"/>
                    </li>
                    <li class="separator"><p:separator/><a id="closer" href="#" class="closerclass ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b" data-rel="back"/></li> 
                    <li data-icon="power">
                        <p:commandLink value="Logout" action="#{loginBean.logout}"
                            styleClass="bordercolorddd"
                            ajax="true" partialSubmit="true"
                            process="@this" />
                    </li>
                </ul>


回答3:

you can use $( "#nav" ).panel( "close" );