0

我正在使用这个 HTML 代码:

<head>

<link href="defualt.css" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="alternate stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="alternate stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="alternate stylesheet" type="text/css"  />

<script type="text/javascript" src="javascript.js"></script>

</head>
<body>

<form>
    <select id="myList" >
        <option value="default">Default</option>
        <option value="theme1">Theme 1</option>
        <option value="theme2">Theme 2</option>  
        <option value="theme3">Theme 3</option>
    </select>
</form>

</body>

这是更改样式表的一种形式。这是我正在使用的 javascript:

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  } 
  createCookie("style", title, 7);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


function initate()
{

document.getElementById("myList").onchange = function() {
   setActiveStyleSheet(this.value);
   return false
};

}

window.onload = initate;

样式表的更改效果很好,但我希望在重新加载页面后保留菜单中选择的选项。这在 Firefox 中可以正常工作,但在 Chrome 中却不行。我对 javascripts 很陌生,而且我自己还没有编写所有这些代码,所以我并不完全理解所有这些代码。是否可以将所选的菜单选项保存在同一个 cookie 中,还是创建一个新的更好/更容易?

4

1 回答 1

1

如果您正在重新加载页面,则无法在页面(HTML、Javascript)本身内保持下拉选择的状态。我建议您使用 cookie 来存储当前选择,在页面加载时读取值,并相应地在下拉菜单中设置选项。

cookie JS,见:http ://www.quirksmode.org/js/cookies.html

function initate() {
    window.selectBox = document.getElementById("myList");
    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
    getTheme();
    selectBox.onchange = function () {
        setActiveStyleSheet(this.value);
        setTheme();
    };
}
window.onload = initate;

function setActiveStyleSheet(title) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]) ; i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
    createCookie("style", title, 7);
}

function getActiveStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]) ; i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
}

function getPreferredStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]) ; i++) {
        if (a.getAttribute("rel").indexOf("style") != -1
           && a.getAttribute("rel").indexOf("alt") == -1
           && a.getAttribute("title")
           ) return a.getAttribute("title");
    }
}

function getTheme() {
    var storedThemeVal = readCookie('selectedTheme');
    if (storedThemeVal != null && storedThemeVal != "") {
        for (var i = 0; i < selectBox.options.length; i++) {
            if (selectBox.options[i].value == storedThemeVal) {
                selectBox.selectedIndex = i;
            }
        }
    }
}

function setTheme() {
    var selectedThemeVal = selectBox.options[selectBox.selectedIndex].value;
    createCookie('selectedTheme', selectedThemeVal);
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

请注意,Chrome 默认不支持本地 cookie(file:// 协议)。要么在服务器环境中进行测试,要么通过添加--enable-file-cookies到 Chrome 的快捷方式来启用文件 cookie(因此,如果您有快捷方式,请右键单击它,单击属性并编辑Target--enable-file-cookies在最后包含引号后包含)。

或者,您也可以修改 URL(用户查询字符串参数)以存储/读取当前选择(类似于上面)。但是,如果用户离开该站点并返回该站点,这当然会丢失。

使用 JavaScript 向 URL 添加参数

于 2013-01-15T14:50:13.063 回答