如何在没有服务器端脚本的情况下在 jquerymobile html5 中编写可重用代码?
有什么方法可以在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 HTML (
.html
) 页面并且没有连接服务器端包含,那么您可以使用 JavaScript 模板(这并不太困难)。使用 JavaScript 进行模板化的另一个好处是,传输的线上数据较少,因此页面下载速度会更快。如果您发现自己一遍又一遍地重新编码相同的内容,那么使用模板是个好主意。以下是在 jQuery Mobile 创建页面时向每个页面的标题添加内容的示例:
另一种选择是使用带有
seemless
属性的标记(已添加) HTML5 规范中)。这是一个文档链接:http://www.w3schools.com/html5/tag_iframe.asp
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:
Another option is to use
<iframe>
tags with theseemless
attribute (added in the HTML5 specification). Here is a doc link: http://www.w3schools.com/html5/tag_iframe.asp这是基于 Jasper 使用 js 模板原理的答案对我有用的解决方案。使用“加载”或类似技术的类似问题的所有其他答案都无法正常工作,因为 html 未正确增强以及其他问题。
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.