无法使用 jquery mobile 更改 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
}
});
根据文档,它应该可以工作。我也尝试过
$("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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用jquery data api:
参考:http://api.jquery.com/data/
try using jquery data api:
refer to: http://api.jquery.com/data/
您需要使用另一个事件,因为在
pageinit
上页面已经得到增强并且忽略您的附加属性。http://jquerymobile.com/test/docs/api/events.html
尝试使用
pagecreate
或pagebeforecreate
代替。这应该有效:
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
orpagebeforecreate
instead.This should work: