1

因此,当您第一次访问我的页面时,我正在尝试加载一个名为one.css的默认样式表。除此之外,我是能够选择主题的用户。我的逻辑的问题是,当您切换到另一种样式时, one.css 似乎正在重置本地存储,就好像这是您第一次访问该网站一样。

是的,我确实意识到逻辑是错误的,希望有人能理清我正在尝试做的事情。

var firstRun = (localStorage['styler'] == 'one');

if (!firstRun) {
  localStorage['styler'] = 'one';
  var style = localStorage["styler"];
  $('<link rel="stylesheet" type="text/css" href="'+style + '.css">').appendTo("head");
}   

else{
 var style = localStorage["styler"];
  $('<link rel="stylesheet" type="text/css" href="'+style + '.css">').appendTo("head");
}
4

2 回答 2

1

我认为您正在寻找:

var style = localStorage['styler'];

if (typeof style === 'undefined'){
   style = 'one';
   localStorage['styler'] = style;
   $('<link rel="stylesheet" type="text/css" href="'+ style + '.css">').appendTo("head");
}
else{
   $('<link rel="stylesheet" type="text/css" href="'+ style + '.css">').appendTo("head");
}
于 2012-06-25T17:51:26.667 回答
0

你总是有变量 style == "one"

var firstRun = (localStorage['styler'] == 'one');

if (!firstRun) {
  var style = localStorage["styler"];
  if(typeof style === 'undefined') style = 'two'
  $('<link rel="stylesheet" type="text/css" href="'+style + '.css">').appendTo("head");
  localStorage['styler'] = 'one';
}   

else{
  var style = localStorage["styler"];
  $('<link rel="stylesheet" type="text/css" href="'+style + '.css">').appendTo("head");
}
于 2012-06-25T17:54:00.040 回答