无法使用jquery mobile更改attr

时间:2011-11-11 02:46:03

标签: jquery jquery-mobile themes fullscreen attr

为什么这段代码不起作用:

$("#page1").live('pageinit', function() {
    if (localStorage.fullscreen == "true") {
        $("#page1").attr("data-fullscreen", "true"); // doesn't work
        alert("should be fullscreen now"); // this works
    }
    if (localStorage.theme) {
        $("header").attr("data-theme", localStorage.theme); // this doesn't work
        alert("should be theme " + localStorage.theme); // this works
    }
 });

According to the docs,它应该有效。我也试过了

$("header").data("theme", localStorage.theme); 

但这也不起作用。

2 个答案:

答案 0 :(得分:0)

尝试使用jquery data api:

 $("#page1").data("fullscreen", "true");
 $("header").data("theme", localStorage.theme);

请参阅:http://api.jquery.com/data/

答案 1 :(得分:0)

您需要使用其他活动,因为在pageinit页面已经增强并忽略了您的其他属性。

http://jquerymobile.com/test/docs/api/events.html

请尝试使用pagecreatepagebeforecreate

这应该有效:

$("#page1").live('pagecreate', function() {
    if (localStorage.fullscreen == "true") {
        $("#page1").attr("data-fullscreen", "true"); // doesn't work
        alert("should be fullscreen now"); // this works
    }
    if (localStorage.theme) {
        $("header").attr("data-theme", localStorage.theme); // this doesn't work
        alert("should be theme " + localStorage.theme); // this works
    }
});