我没有一些 jquery 页面,每个页面都有页眉和页脚。我如何将其设为单个页眉和页脚。 (ie) 想要在主页页眉/页 footer 分加载页眉/页脚 html 文件。
请指教。
请在下面找到页面结构....
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Single page template</title>
<link rel="stylesheet" href="css/jquery.mobile-1.2.0.min.css" />
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/customScript.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div id="hdr">
<!--Need to include header.html-->
</div>
<div data-role="content">
<!--Content-->
</div>
<div id="ftr">
<!--Need to include footer.html-->
</div>
</div>
</body>
</html>
提前致谢....
最佳答案
这是一个工作示例:
index.html
<!DOCTYPE html>
<html>
<head>
<title>jQM Complex Demo</title>
<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.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', '[data-role="page"]', function(){
$.mobile.activePage.find('[data-role="header"]').load("header.html", function(){
$(this).parent().trigger('pagecreate');
});
$.mobile.activePage.find('[data-role="footer"]').load("footer.html", function(){
$(this).parent().trigger('pagecreate');
});
});
</script>
</head>
<body>
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
</div>
<div data-role="content">
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
</body>
</html>
header.html
<h3>
Header
</h3>
footer.html:
<h3>
Footer
</h3>
这里的技巧是在通过load 函数添加内容后触发pagecreate(而不是create)来设置页眉和页脚的样式。
阅读此 article/answer找出两者之间的区别
trigger('create')
和
trigger('pagecreate').
基本上,pagecreate 会重新设计整个页面的样式,而 create 只会调整内容的样式。
关于jquery 移动页眉/页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15788178/