Do I have to create a new panel for every page?

后端 未结 11 1480
Happy的楠姐
Happy的楠姐 2020-12-13 22:30

I would like to use a Panel in a jqm site for my Choose Language component. So that component will need to be present on every page. Here is the setup for a single-page pane

11条回答
  •  别那么骄傲
    2020-12-13 23:21

    I've been wrestling with this myself, and here's the solution I'm using:

    $( ".page" ).on(
        "pageshow",
        function ( event, prevPage ) {
            var $page = $( this ),
                $prevPage = $( prevPage.prevPage ),
                $menuPanel = $( "#panel-menu", $prevPage );
    
            $menuPanel
                .remove()
                .prependTo( $page );
    
            $page.trigger( "create" );
        }
    );
    

    The trick seems to be to use .remove() instead of .detach() to pick up the panel you want to move - without any stored jQuery objects - so that jQuery Mobile doesn't make assumptions about the markup. Also, it strikes me that this solution adds considerable overhead from DOM manipulation. It's not ideal, but it does work.

    EDIT: Note that it's set up to work for every page, and is fired on loading the first page, where you presumably have the menu to start with and where there is no previous page object. But you probably could keep the menu elsewhere and look for it there too as a fallback.

提交回复
热议问题