带有PageContainer的动态加载面板

时间:2016-07-06 20:28:02

标签: javascript jquery html css jquery-mobile

我知道这个问题已被多次询问,但我想为所有页面加载一个面板动态,但它不起作用。 有时css样式没有加载或面板无法打开.. 有什么建议? ;(

    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css"
          href="css/jquery.mobile-1.4.5/jquery.mobile.black-sidebar-theme.css">
    <link rel="stylesheet" type="text/css"
          href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <link rel="stylesheet" type="text/css" href="css/own.css">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript"
            src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <script type="text/javascript">
        $(document).one('pagebeforecreate', function () {


            $.mobile.pageContainer.find("div[data-role=page]").each(function () {
                var panelHtml = $('<div data-role="panel" data-display="overlay" data-theme="g" id="panel_' + this.id + '">').load("templates/panel/panel.html", function (data) {
                });
                $(this).append(panelHtml);
                $("#panel_" + this.id).panel();
                $("#panel_" + this.id).panel().enhanceWithin();
                $("#panel_" + this.id).enhanceWithin();
            });

        });
    </script>
    </head>
    <body>
    <div data-role="page" id="page1">
    <div data-role="content">
        <h2>Content</h2>
        <a href="#panel_page1">Open Panel</a>
    </div>
    </div>
    <div data-role="page" id="page2">
    <div data-role="content">
        <h2>Content</h2>
    </div>
    </div>
    <div data-role="page" id="page3">
    <div data-role="content">
        <h2>Content</h2>
    </div>
   </div>
   </body>
   </html>

我的panel.html

<ul data-role="listview" data-theme="g" data-divider-theme="g">
<li data-role="list-divider">Panel</li>
<li><a href="#searchPage">Page1</a></li>
<li><a href="#downloadPage">Page2</a></li>
<li><a href="#playerPage">Pag3</a></li>
</ul>

enter image description here

2 个答案:

答案 0 :(得分:1)

感谢您的提示...... .one('pagebeforecreate',

在所有页面中加载相同的页眉和/或页脚并不容易。

找到合适的事件确实很困难。

我稍微修改了你的解决方案,它更简洁,速度更快。

$(document).one('pagebeforecreate', function () {

    $.get('header.html').success(function(htmlHeader){
        $.get('footer.html').success(function(htmlFooter){

            $(htmlHeader).prependTo($('[data-role="page"]'));
            $(htmlFooter).appendTo($('[data-role="page"]'));
            $('body').enhanceWithin(); // All pages in once
        });
    });
});

答案 1 :(得分:0)

@thanks to ezanker
- 我不知道外部面板存在,谢谢 - 我在jsfiddle上测试它,但它工作但我没有在当地工作,我不知道为什么

这就是我的解决方案
当你知道外部面板存在时,它有点傻,但这里是

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css"
          href="css/jquery.mobile-1.4.5/jquery.mobile.black-sidebar-theme.css">
    <link rel="stylesheet" type="text/css"
          href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <link rel="stylesheet" type="text/css" href="css/own.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript"
            src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <script type="text/javascript">
        $(document).one('pagebeforecreate', function () {

            $.mobile.pageContainer.find("div[data-role=page]").each(function () {
                var id = this.id;
                var html = $('<div data-role="panel" data-display="overlay" data-theme="g" id="panel_' + this.id + '">').load("templates/panel/panel.html", function (data) {
                    $("#" + id).append(html);
                    $("#" + id).enhanceWithin();
                });
            });

            $.mobile.pageContainer.find("div[data-role=page]").each(function () {
                var id = this.id;
                var html = $('<div data-role="header">').load("templates/header/header.html", function () {
                    $("#" + id).prepend(html);
                    $("#" + id).enhanceWithin();
                });
            });

            $.mobile.pageContainer.find("div[data-role=page]").each(function () {
                var id = this.id;
                var html = $('<div data-role="footer" id="footer" data-position="fixed" data-tap-toggle="false">').load("templates/footer/footerUp.html", function () {
                    $("#" + id).append(html);
                    $("#" + id).enhanceWithin();
                });
            });

            $.mobile.pageContainer.find("div[data-role=page]").each(function () {
                var id = this.id;
                var html = $('<div data-role="popup" id="popup_' + this.id + '" data-theme="a" class="ui-corner-all">').load("templates/popup/type_1/popup.html", function () {
                    $("#" + id).append(html);
                    $("#" + id).enhanceWithin();
                });
            });
        });
    </script>
</head>
<body>
<div data-role="page" id="page1">
    <div data-role="content">
        <h2>Content</h2>
        <a href="#panel_page1">Open Panel</a>
        <a href="#" onclick='$("#popup_page1").popup("open")'>Open PopUp</a>
    </div>
</div>
<div data-role="page" id="page2">
    <div data-role="content">
        <h2>Content</h2>
        <a href="#panel_page2">Open Panel</a>
        <a href="#" onclick='$("#popup_page2").popup("open")'>Open PopUp</a>
    </div>
</div>
<div data-role="page" id="page3">
    <div data-role="content">
        <h2>Content</h2>
        <a href="#panel_page3">Open Panel</a>
        <a href="#" onclick='$("#popup_page3").popup("open")'>Open PopUp</a>
    </div>
</div>
</body>
</html>