1

我知道也有类似的问题被问和回答。但我的问题是不同的。这是我用来加载和样式化我的内容的代码示例。

 $("body").on("click","[data-view]",function(){
        var view=$(this).attr("data-view");
        $("#mainView").load("pageRouter.php?view="+view).trigger("create");

    });

我搜索了论坛,发现.trigger("create")是对动态加载的内容进行风格化的方法。但它没有帮助。

4

1 回答 1

1

解决方案

您做错了,加载是异步功能,因此您需要等待它加载内容才能增强内容。

您将需要这样做:

$("#mainView").load("pageRouter.php?view="+view, function() {
   $(this).trigger("create");
});

如果您只加载内容,但如果您正在加载带有页眉和页脚的完整页面,则这将起作用,然后使用:

$("#mainView").load("pageRouter.php?view="+view, function() {
   $(this).trigger("pagecreate");
});

在此处阅读有关铅及其回调的更多信息。

工作示例:

索引.php

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <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.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
        <script>
            $(document).on('pagebeforeshow', '#index', function(){ 
                $("#index").load("load.php", function() {
                    $(this).trigger("pagecreate");
                });
            });
        </script>
    </head>
    <body>
        <div data-role="page" id="index">

        </div>    
    </body>
</html>   

加载.php

<div data-theme="b" data-role="header">
    <h1>Index page</h1>
</div>

<div data-role="content">
    <a data-role="button">Button</a>
</div>
于 2013-06-01T09:44:00.403 回答