jQUery Mobile 共享页眉和页脚

标签 jquery jquery-mobile cordova

我正在创建一个phonegap应用程序,并且我正在尝试在所有页面之间共享页眉和页脚代码。我想使用 jqm 中的内部链接,但他们的指南说每个子页面都有一个标题内容和页面,例如:

<div data-role="page" id="one">

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

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

        <p>I have an <code>id</code> of "one" on my page container. I'm first in the source order so I'm shown when the page loads.</p> 

        <p>This is a multi-page boilerplate template that you can copy to build your first jQuery Mobile page. This template contains multiple "page" containers inside, unlike a <a href="page-template.html"> single page template</a> that has just one page within it.</p>  
        <p>Just view the source and copy the code to get started. All the CSS and JS is linked to the jQuery CDN versions so this is super easy to set up. Remember to include a meta viewport tag in the head to set the zoom level.</p>
        <p>You link to internal pages by referring to the <code>id</code> of the page you want to show. For example, to <a href="#two" >link</a> to the page with an <code>id</code> of "two", my link would have a <code>href="#two"</code> in the code.</p>   

        <h3>Show internal pages:</h3>
        <p><a href="#two" data-role="button">Show page "two"</a></p>    
        <p><a href="#popup" data-role="button" data-rel="dialog" data-transition="pop">Show page "popup" (as a dialog)</a></p>
    </div><!-- /content -->

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


<!-- Start of second page: #two -->
<div data-role="page" id="two" data-theme="a">

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

    <div data-role="content" data-theme="a">    
        <h2>Two</h2>
        <p>I have an id of "two" on my page container. I'm the second page container in this multi-page template.</p>   
        <p>Notice that the theme is different for this page because we've added a few <code>data-theme</code> swatch assigments here to show off how flexible it is. You can add any content or widget to these pages, but we're keeping these simple.</p>  
        <p><a href="#one" data-direction="reverse" data-role="button" data-theme="b">Back to page "one"</a></p> 

    </div><!-- /content -->

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

这很好......但我想为每个页面使用相同的数据角色页脚和页眉。复制相同的代码似乎毫无意义(因为每个页面的设计等都是相同的?)。这是针对phonegap应用程序的,每个页面都有很多html,因此这将是大量愚蠢的重复(拉出菜单等)

正确的做法是什么?我的想法是这样的:

<div data-role="page" id="bar">

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

    <div data-role="content" page=1>    
        <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>      
        <p><a href="#foo">Back to foo</a></p>   
    </div><!-- /content -->

    <div data-role="content" page=2>    
        <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>      
        <p><a href="#foo">Back to foo</a></p>   
    </div><!-- /content -->

    <div data-role="content" page=3>    
        <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>      
        <p><a href="#foo">Back to foo</a></p>   
    </div><!-- /content -->

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

最佳答案

jQuery Mobile 定义了页面结构,其中包括页面包装 div[data-role=page] 因此,如果您想为所有页面使用通用页眉或页脚,那么您需要注入(inject)页眉/页脚元素动态地添加到当前页面。您可以使用 jquery 库方法在页面中附加元素。 这是一些例子

var currentPage=$.mobile.activePage;

$('<div data-role="header" id="myheader">
        <a href="" >Home</a>
        <a href="" >Back</a>
    </div>').prependTo(currentPage);

$('<div data-role="footer" id="myfooter">
        <a href="">About Us</a>
    </div>').appendTo(currentPage);

您可以在jquery mobile中调用触发器create事件或refresh事件来增强元素。

关于jQUery Mobile 共享页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23572864/

相关文章:

javascript - 没有中间证书的 SSL 会导致签名的 APK 无法加载外部 CSS/JS 吗?

asp.net-mvc - 使用 ASP.NET MVC 的 BlueImp jQuery 上传

jquery - 在 JQuery 中使用 Load() 获取 XML 属性

javascript - 在页面加载时调用 Knockout JS 鼠标悬停事件

jquery-mobile - 将文本换行在列表项/按钮中,而不是 overflow hidden

jquery - 刷新jquery mobile中动态创建的页面

javascript - ionic 服务在 Chrome : Illegal constructor in CreateClass in core. js 中给出错误

javascript - Backbone.js 渲染性能和重绘

css - 显示带有选择器的对话框的 jquery 移动排序按钮

android - phonegap - navigator.connection.type 未定义