【问题标题】:jquery mobile header/footerjquery移动页眉/页脚
【发布时间】:2014-12-05 17:55:13
【问题描述】:

我没有一些 jquery 页面,每个页面都有页眉和页脚。我如何使它成为单个页眉和页脚。 (ie) 想要在主页的页眉/页脚部分加载页眉/页脚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>

提前谢谢....

【问题讨论】:

  • 首先,&lt;head&gt; 中应该有 JQM 脚本和样式表链接。
  • 我在我的移动开发者上使用 php 与 jQuery mobile 一起使用 header.php 文件和另一个 footer.php .然后我在每个需要的文件上用&lt;?php include 'inc/header.php'; ?&gt; 给他们打电话。
  • php 将如何在移动设备中工作...?你能提供一些代码吗...
  • @Yesvinkumar PHP 在服务器中工作,因此它独立于前端。无论如何,我想您应该阅读一些 Web 开发介绍性概念。

标签: jquery html jquery-mobile markup


【解决方案1】:

这是一个工作示例:

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 将只设置内容的样式。

【讨论】:

  • 嗨 Gajotres,感谢分享...这个解决方案对我来说很好!
  • @Yesvinkumar,也许你应该把它标记为答案。
【解决方案2】:

你可以使用jQuery load():

$(document).ready(function() {
    $("#hdr").load("header.html"); 
    $("#ftr").load("footer.html"); 
});

【讨论】:

  • 我也用过。但是即使我添加触发器('create'),样式也不会更新。如果有的话,你能提供一些代码示例吗?非常感谢...
【解决方案3】:

添加标题

检查是否没有标头,然后检查.append()标头.before() div data-role=content

$(document).on('pagebeforeshow', function () {
 if ($(this).find('[data-role=header]').length == 0) {
   $('[data-role=content]').before('<div data-role="header"><h1>Page header</h1></div>');
   $('[data-role=page]').trigger('pagecreate');
 }
});

添加页脚

检查是否没有页脚,然后检查.append() 页脚.after() div data-role=content

$(document).on('pagebeforeshow', function () {
 if ($(this).find('[data-role=footer]').length == 0) {
  $('[data-role=content]').after('<div data-role="footer"><h1>Page footer</h1></div>');
  $('[data-role=page]').trigger('pagecreate');
 }
});

【讨论】:

    【解决方案4】:
    var currentPage=$.mobile.activePage;
    
    $('<div data-role="header" id="myheader">
            <a href="" >Home</a>
            <a href="" >Back</a>
        </div>').prependTo(currentPage).trigger('create');
    
    $('<div data-role="footer" id="myfooter">
            <a href="">About Us</a>
        </div>').appendTo(currentPage).trigger('create');
    

    【讨论】:

      猜你喜欢
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多