2011-11-11 71 views
1

改變ATTR爲什麼沒有此代碼的工作:不能與jQuery Mobile的

$("#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); 

但這也行不通。

+0

你的意思是 「不工作」?這些屬性很可能已經設置好了,但這不會改變頁面中的任何內容,除非您運行其他代碼或者使用CSS來查看這些屬性。 – jfriend00

+0

你有沒有在不同的瀏覽器中試過這個? – sally

回答

0

嘗試使用jQuery數據API:

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

參考:http://api.jquery.com/data/

+0

謝謝。我嘗試過,出於某種原因全屏工作,但主題沒有。任何想法,爲什麼會這樣? –

+0

你的標題是否有屬性「data-role ='page'」? _ data-theme屬性可應用於頁眉和頁腳容器以應用任何字母主題顏色色板。雖然可以將data-theme屬性添加到內容容器,但我們建議將其添加到已分配了data-role =「page」屬性的div或容器,以確保將背景色應用於整個頁面。 [jQuery Mobile Docs](http://jquerymobile.com/demos/1.0rc2/docs/pages/pages-themes.html) –

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 
    } 
});