Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

ajax - jQuery Mobile does not apply styles after dynamically adding content

I know this questions appear several places (Forcing jQuery Mobile to re-evaluate styles/theme on dynamically inserted content) but not with an answer that is working for me.

I'm loading some content using ajax, and inserting it into a div like this:

       $.ajax({
            url: "../Services/CalendarService.cshtml?service=true",
            cache: false,
            success: function (data) {

                data = $.parseJSON(data);
                var s = $("#user_tmpl").html();
                var s1 = tmpl(s, data);

                $("#target").html(s1);
                $("#targetRefresh").page();
            }
        });

I've tried setting the targetRefresh on both the target I'm adding the html to, and on the page, but with no luck. The conent is inserted, but styles not applied.

I've also tried

.trigger("enhance")

Any idea what to do?

The html that inserted are a bunch of these:

<div data-theme="e" data-collapsed="true" data-role="collapsible">         <h3>MyOwner2AA</h3>         <p>MyDescription</p>         <p>/Date(1320339836735)/</p>         <p>MyOwner</p>         <i></i>     </div>

Thanks for any help

Larsi

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try calling .trigger("create") on the element with the new content.

According to the jQuery Mobile docs, "The create event is suited for enhancing raw markup that contains one or more widgets."

EDIT: As of jQuery Mobile 1.4, .trigger('create') is deprecated, and you should use .enhanceWithin() instead. (Thanks to John Mc for the heads-up.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...