无法使用 jquery mobile 更改 attr

发布于 2024-12-15 01:30:05 字数 684 浏览 3 评论 0原文

为什么此代码不起作用:

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

,但这也不起作用。

Why doesn't this code work:

$("#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, it should work. I've also tried

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

but that doesn't work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不咬妳我踢妳 2024-12-22 01:30:05

尝试使用jquery data api:

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

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

try using jquery data api:

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

refer to: http://api.jquery.com/data/

琉璃繁缕 2024-12-22 01:30:05

您需要使用另一个事件,因为在 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
    }
});

You need to use another event, because on pageinit the page already got enhanced and is ignoring your additional attributes.

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

Try using pagecreate or pagebeforecreate instead.

This should work:

$("#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
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文