强制jQuery Mobile重新评估动态插入内容的样式/主题

T. *_*one 55 jquery-mobile

目标: 通过加载HTML内容$.ajax,将其插入DOM,让jQuery Mobile应用主题样式.

问题: 内容被插入但缺少jQuery Mobile主题.

码:

$.ajax({
    ...
    success: function(html) {
        $('#container').append(html);
        $('#page').page('refresh', true);
    }
});
Run Code Online (Sandbox Code Playgroud)

返回的HTML包含data-rolejQM应该样式的标签......

<a data-role="button">Do Something</a>
Run Code Online (Sandbox Code Playgroud)

我没有应用它应该的样式,而是出现以下错误:

未捕获的异常:没有这样的方法'刷新'页面小部件实例


以上代码测试使用 http://code.jquery.com/mobile/latest/jquery.mobile.js


类似的问题让我收到上述错误信息:

使用适当的jQuery Mobile样式一致地更新页面

JQM(jQueryMobile)动态添加了未正确显示的元素,并且未应用CSS

jQuery Mobile - 动态创建表单元素

Lar*_*rsi 66

刚刚得到一个类似问题的答案,尝试使用

.trigger("create")
Run Code Online (Sandbox Code Playgroud)

在获取内容的元素上.

请参阅此处:jQuery Mobile在动态添加内容后不应用样式


dou*_*uyw 12

如果将项添加到列表视图,则需要在其上调用refresh()方法以更新样式并创建添加的任何嵌套列表.例如:

$('#mylist').listview('refresh');
Run Code Online (Sandbox Code Playgroud)

如果您需要呈现动态页面,请阅读:" jQuery Mobile和Dynamic Page Generation ".本文的示例代码:

// Load the data for a specific category, based on
// the URL passed in. Generate markup for the items in the
// category, inject it into an embedded page, and then make
// that page the current active page.
function showCategory( urlObj, options )
{
    var categoryName = urlObj.hash.replace( /.*category=/, "" ),

        // Get the object that represents the category we
        // are interested in. Note, that at this point we could
        // instead fire off an ajax request to fetch the data, but
        // for the purposes of this sample, it's already in memory.
        category = categoryData[ categoryName ],

        // The pages we use to display our content are already in
        // the DOM. The id of the page we are going to write our
        // content into is specified in the hash before the '?'.
        pageSelector = urlObj.hash.replace( /\?.*$/, "" );

    if ( category ) {
        // Get the page we are going to dump our content into.
        var $page = $( pageSelector ),

            // Get the header for the page.
            $header = $page.children( ":jqmData(role=header)" ),

            // Get the content area element for the page.
            $content = $page.children( ":jqmData(role=content)" ),

            // The markup we are going to inject into the content
            // area of the page.
            markup = "<p>" + category.description + "</p><ul data-role='listview' data-inset='true'>",

            // The array of items for this category.
            cItems = category.items,

            // The number of items in the category.
            numItems = cItems.length;

        // Generate a list item for each item in the category
        // and add it to our markup.
        for ( var i = 0; i < numItems; i++ ) {
            markup += "<li>" + cItems[i].name + "</li>";
        }
        markup += "</ul>";

        // Find the h1 element in our header and inject the name of
        // the category into it.
        $header.find( "h1" ).html( category.name );

        // Inject the category items markup into the content element.
        $content.html( markup );

        // Pages are lazily enhanced. We call page() on the page
        // element to make sure it is always enhanced before we
        // attempt to enhance the listview markup we just injected.
        // Subsequent calls to page() are ignored since a page/widget
        // can only be enhanced once.
        $page.page();

        // Enhance the listview we just injected.
        $content.find( ":jqmData(role=listview)" ).listview();

        // We don't want the data-url of the page we just modified
        // to be the url that shows up in the browser's location field,
        // so set the dataUrl option to the URL for the category
        // we just loaded.
        options.dataUrl = urlObj.href;

        // Now call changePage() and tell it to switch to
        // the page we just modified.
        $.mobile.changePage( $page, options );
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 10

如果您使用ajax方法加载到内容中,这就是我使用样式和jquery移动功能的方法.它与上面的建议几乎相同,但对于某些人,您可能希望看到更完整的示例.

这是代码:

$.ajax({
url: 'url.php',
success: function(data) {    
$("#div").html(data).trigger('create');
}
});
Run Code Online (Sandbox Code Playgroud)


T. *_*one 0

对于其他正在寻找此问题答案的人,截至 2011 年 6 月 9 日,jQuery 移动团队已在开发分支中实现了此功能。根据这个问题,它将在这个庄园工作:

$(".ui-content").append( ... lots of HTML ...).trigger( "enhance" );

https://github.com/jquery/jquery-mobile/issues/1799

  • 这还太早了。我知道您想成为第一个发布该功能的人,但代码尚未发布,如果您不提供有关具有此功能的版本的任何信息,可能会造成很多混乱。 (6认同)