将外部Html合并到jQuery Mobile页面中

Ben*_*rce 3 html load external inject jquery-mobile

我试图动态地将外部html源合并到jQuery移动页面中.我能够成功地整合外部html,但它看起来像常规HTML(即不是jQuery mobile影响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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Jas*_*per 8

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

$("#main").load('externalHtml.html').trigger('create');
Run Code Online (Sandbox Code Playgroud)

他们确实应该更好地记录这一点,但如果你查看每种类型的小部件的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');
});
Run Code Online (Sandbox Code Playgroud)