не могу изменить attr с помощью jquery mobile - PullRequest
1 голос
/ 11 ноября 2011

Почему этот код не работает:

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

Согласно документации , это должно работать. Я также попробовал

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

но это тоже не работает.

Ответы [ 2 ]

0 голосов
/ 12 ноября 2011

Вам нужно использовать другое событие, потому что на pageinit страница уже улучшена и игнорирует ваши дополнительные атрибуты.

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

Попробуйте использовать pagecreate или pagebeforecreate.

Это должно работать:

$("#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
    }
});
0 голосов
/ 11 ноября 2011

попробуйте использовать jquery api:

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

см. http://api.jquery.com/data/

...