【问题标题】:Incorporating External Html into a jQuery Mobile page将外部 Html 合并到 jQuery Mobile 页面中
【发布时间】:2012-07-31 22:43:09
【问题描述】:

我正在尝试将外部 html 源动态合并到 jQuery 移动页面中。我能够成功合并外部 html,但它看起来像常规 HTML(即不受 jQuery 移动影响的 Html)。谁能建议我可能做错了什么?

主 HTML:

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css"
    />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://jqueryui.com/ui/jquery-1.7.1.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#main").load('externalHtml.html');
            //$("#main").append('externalHtml.html');
            //$("#main").load('externalHtml.html #contain');
            //$("#main").page();
        });
    </script>
</head>

<body>
    <div data-role="content">
        <div id="main"></div>Main Page</div>
</body>

externalHtml.html:

<html>
<head>
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
                <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css"/>
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script src="http://jqueryui.com/ui/jquery-1.7.1.js"></script>
        <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
 </head>
<body>
external html
<div data-role="content" id="contain">
<input  type="search" name="name" id="name" value="" />
 </div>
      </body>
      </html>

【问题讨论】:

    标签: html jquery-mobile load external inject


    【解决方案1】:

    如果您在容器元素上.trigger('create'),jQuery Mobile 将自动初始化容器内的任何小部件。例如:

    $("#main").load('externalHtml.html').trigger('create');
    

    他们确实应该更好地记录这一点,但是如果您查看每种类型的小部件的 API 事件,您将看到有关 create 事件的文档。

    另外,请阅读文档页面的顶部:http://jquerymobile.com/demos/1.1.1/docs/api/events.html

    您不应该使用document.ready,而应该绑定到伪页面的pageinit 事件。将来使用document.ready 很可能会让您头疼。

    -- 更新--

    您可能希望在回调中调用.trigger('create'),以便在您尝试初始化之前加载外部 HTML:

    $("#main").load('externalHtml.html', function () {
        $(this).trigger('create');
    });
    

    【讨论】:

    • @Magico 您可以使用 window.location.hash = 'some-hash'; 更新 URL,或者在现代浏览器中,您可以使用 pushState API 更新哈希之外的 URL。
    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 2017-12-03
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多