【问题标题】:How to show the second page as the default page in a jQuery mobile Multi-page template structure?如何在 jQuery 移动多页模板结构中将第二页显示为默认页面?
【发布时间】:2013-05-19 23:20:30
【问题描述】:

如何在 jQuery 移动多页模板结构中将第二页显示为默认页面?

<body> 
<div data-role="page" id="foo">
    <div data-role="header">
        <h1>Foo</h1>
    </div>
    <div data-role="content">   
        <p>I'm first in the source order so I'm shown as the page.</p>      
    </div>
</div>
<div data-role="page" id="home">
    <div data-role="header">
        <h1>Home</h1>
    </div>
    <div data-role="content">   
        <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
    </div>
</div>
<div data-role="page" id="bar">
    <div data-role="header">
        <h1>Bar</h1>
    </div>
    <div data-role="content">   
        <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
    </div>
</div>
</body>

【问题讨论】:

    标签: javascript jquery jquery-mobile


    【解决方案1】:

    示例

    工作示例:http://jsfiddle.net/Gajotres/UfDaf/

    使用的代码

    这部分会阻止正常的页面加载。不要忘记,在我的示例中,mobileinit 必须jQuery Mobile 初始化之前进行初始化。

    $(document).on("mobileinit",function() {
        $.mobile.autoInitializePage = false;
    }); 
    

    这部分将开始手册页初始化:

    $(document).ready(function() {            
        window.location.hash = 'home';
        $.mobile.initializePage();
    });
    

    虽然我通常 advise 反对此处的文档准备使用,但我们需要它来启动手动更改。

    完整代码示例

    万一 jsFiddle 宕机。

    HTML:

    <!DOCTYPE html>
    <html>
        <head>
            <title>jQM Complex Demo</title>
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
            <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
            <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
            <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
            <script>
                $(document).on("mobileinit",function() {
                    $.mobile.autoInitializePage = false;
                });        
            </script>
            <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
        </head>
        <body>
            <div data-role="page" id="foo">
                <div data-role="header">
                    <h1>Foo</h1>
                </div>
                <div data-role="content">   
                    <p>I'm first in the source order so I'm shown as the page.</p>      
                </div>
            </div>
            <div data-role="page" id="home">
                <div data-role="header">
                    <h1>Home</h1>
                </div>
                <div data-role="content">   
                    <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
                </div>
            </div>
            <div data-role="page" id="bar">
                <div data-role="header">
                    <h1>Bar</h1>
                </div>
                <div data-role="content">   
                    <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
                </div>
            </div>        
        </body>
    </html>   
    

    Javascript:

    $(document).ready(function() {            
        window.location.hash = 'home';
        $.mobile.initializePage();
    });
    

    【讨论】:

    • 如果还设置了$.mobile.hashListeningEnabled = false;,有什么办法吗?
    【解决方案2】:

    我知道这个问题有点老了,但我偶然发现了一个简单的解决方案:

    window.location.href = "_dash.html#page-two";
    

    或者,如果您从单独的页面导航,您可以创建一个链接,将您放在第二页中,如下所示:

    <a href="_dash.html#page-two">link to second page</a>
    

    只需在您的网址末尾添加第二页的页面 ID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多