【问题标题】:Using jQuery Mobile navigation model pages (data-role='page') can I modularize the code by splitting the pages across different files?使用 jQuery Mobile 导航模型页面 (data-role='page') 我可以通过将页面拆分到不同的文件来模块化代码吗?
【发布时间】:2013-09-26 18:20:05
【问题描述】:

下面是一个页面布局的jQuery页面结构。 jQuery mobile 将每个 data-role='page' 视为用户可以浏览的不同页面。

我的项目现在变得相当大,我在同一个 .php 文件上有 10 个 data-role='page' 页面,我想将它们分成 10 个页面,但仍然保留 jQuery Mobile 提供的功能。我怎样才能做到这一点?

<body> 
<!-- Start of first page -->
<div data-role="page" id="foo">

<div data-role="header">
    <h1>Foo</h1>
</div><!-- /header -->

<div data-role="content">   
    <p>I'm first in the source order so I'm shown as the page.</p>      
    <p>View internal page called <a href="#bar">bar</a></p> 
</div>

<div data-role="footer">
    <h4>Page Footer</h4>
</div>
</div><!-- /page -->

<!-- Start of second page -->
<div data-role="page" id="bar">

<div data-role="header">
    <h1>Bar</h1>
</div>

<div data-role="content">       
</div>

<div data-role="footer">
    <h4>Page Footer</h4>
</div>
</div><!-- /page -->
</body>

【问题讨论】:

    标签: jquery jquery-mobile


    【解决方案1】:

    jQuery mobile 有两个页面模板。

    每个页面都放在一个 HTML 文件中,就像您的情况一样。又称多页模板。

    第二个每 1 个 HTML 文件有 1 个页面。称为多HTML模板。

    阅读更多关于他们的信息here

    您需要的是一个多 HTML 模板。这是一个工作示例:

    HTML 1 - index.html:

    <!DOCTYPE html>
      <html>
        <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
        </script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>  
        <script>
            $(document).on('pagebeforeshow', "#index",function () {
                $(document).on('click', "#changePage",function () {     
                    $.mobile.changePage('second.html', { dataUrl : "second.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : false, changeHash : true });
                }); 
            }); 
    
            $(document).on('pagebeforeshow', "#second",function () {
                var parameters = $(this).data("url").split("?")[1];;
                parameter = parameters.replace("parameter=","");  
                alert(parameter);
            });         
        </script>
       </head>
       <body>
        <!-- Home -->
        <div data-role="page" id="index">
            <div data-role="header">
                <h3>
                    First Page
                </h3>
            </div>
            <div data-role="content">
              <a data-role="button" id="changePage">Javascript change page example</a>
                          <a href="second.html" data-transition="slide">Direct link button</a>
            </div> <!--content-->
        </div><!--page-->
    
      </body>
    </html>
    

    HTML 2 - second.html:

    <!DOCTYPE html>
      <html>
        <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js">
        </script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>  
       </head>
       <body>
        <!-- Home -->
        <div data-role="page" id="second">
            <div data-role="header">
                <h3>
                    Second Page
                </h3>
            </div>
            <div data-role="content">
    
            </div> <!--content-->
        </div><!--page-->
    
      </body>
    </html>
    

    您还需要知道一件事。只有第一个 HTML 文件可以有多个内部 data-role="page"。每个其他 HTML 页面只能包含 1 个 data-role="page"。原因描述为 here。如果您需要更多信息,请给我评论。

    【讨论】:

      【解决方案2】:

      使用单页模板

      http://jquerymobile.com/demos/1.3.0-beta.1/docs/pages/page-template.html

      或者您可以将当前拥有的页面拆分为多个 php 文件,然后将它们全部包含到您的主 php 文件中,然后像往常一样输出标记。

      【讨论】:

        【解决方案3】:

        是的,你可以,这是一个例子:

        <li><a href="masterclass.php" data-transition="slide">Master Class</a></li>
        

        【讨论】:

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