如何在没有服务器端脚本的情况下在 jquerymobile html5 中编写可重用代码?

发布于 2024-12-12 06:45:22 字数 682 浏览 1 评论 0原文

有什么方法可以在 jquery mobile 中包含文件,使某些代码可重复使用,而不是到处编写冗余代码?

就像每个页面一样,我需要有一个主页按钮和一个后退按钮......有没有一种方法可以让我在文件中指定这些并将它们包含在每个页面中。我知道使用 javascript 执行此操作的困难方法...我可以在其中放置一个 div="header" 并包含一个稍后添加内容的脚本...

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

    <div data-role="header">

            -----   include the header file to to make it reusable ----

    </div><!-- /header -->

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

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

任何帮助都会受到赞赏...

is there any way i can include files in jquery mobile making some code re-usable instead of writing redundant code everywhere ?

Like for every page i need to have a home button and a back button .... is there a way where i can specify these in a file and include them in every page. i know a hard way of doing this using javascript ... where i can put a div="header" and include a script that will later add content ...

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

    <div data-role="header">

            -----   include the header file to to make it reusable ----

    </div><!-- /header -->

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

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

Any help is appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-19 06:45:22

如果您使用的是 HTML (.html) 页面并且没有连接服务器端包含,那么您可以使用 JavaScript 模板(这并不太困难)。使用 JavaScript 进行模板化的另一个好处是,传输的线上数据较少,因此页面下载速度会更快。如果您发现自己一遍又一遍地重新编码相同的内容,那么使用模板是个好主意。

以下是在 jQuery Mobile 创建页面时向每个页面的标题添加内容的示例:

$('[data-role="page"]').live('pagecreate', function () {
    var $header = $(this).children('[data-role="header"]');
    $header.html('<div data-role="navbar"><ul><li><a href="#">Nav Item 1</a></li><li><a href="#">Nav Item 2</a></li><li><a href="#">Nav Item 3</a></li></ul></div>').trigger('create');
});

另一种选择是使用带有 seemless 属性的 标记(已添加) HTML5 规范中)。这是一个文档链接:http://www.w3schools.com/html5/tag_iframe.asp

Seemless:指定 iframe 应该看起来像是 iframe 的一部分。
包含文档

<div data-role="header">

        <iframe seemless="seemless" src="/path/to/header.html"></iframe>

</div><!-- /header -->

If you are using HTML (.html) pages and do not have Server-Side-Includes hooked-up then you can use a JavaScript template (which is not too difficult). An added benefit of templating with JavaScript is that there is less over-the-wire data being transferred so the pages will download faster. It's a good idea to use templates if you find yourself re-coding the same thing over and over.

Here is an example of adding content to each page's header when the page is created by jQuery Mobile:

$('[data-role="page"]').live('pagecreate', function () {
    var $header = $(this).children('[data-role="header"]');
    $header.html('<div data-role="navbar"><ul><li><a href="#">Nav Item 1</a></li><li><a href="#">Nav Item 2</a></li><li><a href="#">Nav Item 3</a></li></ul></div>').trigger('create');
});

Another option is to use <iframe> tags with the seemless attribute (added in the HTML5 specification). Here is a doc link: http://www.w3schools.com/html5/tag_iframe.asp

Seemless: Specifies that the iframe should look like it is part of the
containing document

<div data-role="header">

        <iframe seemless="seemless" src="/path/to/header.html"></iframe>

</div><!-- /header -->
白昼 2024-12-19 06:45:22

这是基于 Jasper 使用 js 模板原理的答案对我有用的解决方案。使用“加载”或类似技术的类似问题的所有其他答案都无法正常工作,因为 html 未正确增强以及其他问题。

$(document).on('pagebeforecreate', function () {
    // use document so that it triggers for all new pages
    // use pagebeforecreate so that JQM can enhance the html after it is inserted in the DOM
    // use 'on' instead of 'live' as 'live' is deprecated and does not work in jquery 1.9.1
    // $(this) is the current document here so have to use 'find' instead of 'children'
    $(this).find('[data-role="header"]').each(function () {
        // $(this) is now the header
        // if we have already inserted the html, we should not do it anymore
        if ($.trim($(this).html()) === "") $(this).html('<div data-role="navbar"><ul><li><a href="#">Nav Item 1</a></li><li><a href="#">Nav Item 2</a></li><li><a href="#">Nav Item 3</a></li></ul></div>');
    });
});

Here is the solution that worked for me based on Jasper's answer using js template principle. All the other answers to similar questions that use 'load' or similar technics do not work correctly as the html is not enhanced correctly and other issues.

$(document).on('pagebeforecreate', function () {
    // use document so that it triggers for all new pages
    // use pagebeforecreate so that JQM can enhance the html after it is inserted in the DOM
    // use 'on' instead of 'live' as 'live' is deprecated and does not work in jquery 1.9.1
    // $(this) is the current document here so have to use 'find' instead of 'children'
    $(this).find('[data-role="header"]').each(function () {
        // $(this) is now the header
        // if we have already inserted the html, we should not do it anymore
        if ($.trim($(this).html()) === "") $(this).html('<div data-role="navbar"><ul><li><a href="#">Nav Item 1</a></li><li><a href="#">Nav Item 2</a></li><li><a href="#">Nav Item 3</a></li></ul></div>');
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文