Javascript 从localStorage存储和加载下拉选择

Javascript 从localStorage存储和加载下拉选择,javascript,cookies,local-storage,Javascript,Cookies,Local Storage,我正在尝试将一个选项值保存到localstorage,以便在打开其他页面或返回网站时保存该选项,并使用与上次打开网站时相同的css文件 这是我迄今为止所做的,但我一直未能使其发挥作用: HTML: 最终成功地使它发挥了作用。这就是我所做的: HTML: <select name="style" id="style" onChange="changeCSS();"> <option id="standard" value="standard">Standard<

我正在尝试将一个选项值保存到localstorage,以便在打开其他页面或返回网站时保存该选项,并使用与上次打开网站时相同的css文件

这是我迄今为止所做的,但我一直未能使其发挥作用:

HTML:


最终成功地使它发挥了作用。这就是我所做的:

HTML:

<select name="style" id="style" onChange="changeCSS();">
    <option id="standard" value="standard">Standard</option>
    <option id="alternative" value="alternative">Alternative</option>
</select>
function changeCSS() {
    "use strict";
    var select, stylesheet, save;

    select = document.getElementById("style");
    stylesheet = document.getElementById("stylesheet");

    if(localStorage.getItem('save')) {
        select.options[localStorage.getItem('save')].selected = true;
    }

    if (select.value === "standard") {
        stylesheet.href = "include/global.css";
        localStorage.setItem('save', select.value);
    } else if (select.value === "alternative") {
        stylesheet.href = "include/alternative.css";
        localStorage.setItem('save', select.value);
    }
}
<select name="style" id="style" onChange="changeCSS();">
    <option id="standard" value="standard">Standard</option>
    <option id="alternative" value="alternative">Alternative</option>
</select>
<body onload="autoCSS();">
var select, stylesheet;

function changeCSS() {
    "use strict";

    select = document.getElementById("style");
    stylesheet = document.getElementById("stylesheet");

    if (select.value === "standard") {
        stylesheet.href = "include/global.css";
        localStorage.setItem('save', select.value);
    } else if (select.value === "alternative") {
        stylesheet.href = "include/alternative.css";
        localStorage.setItem('save', select.value);
    }
}

function autoCSS() {
    "use strict";

    select = document.getElementById("style");
    stylesheet = document.getElementById("stylesheet");

    if (localStorage.getItem('save')) {
        select.options[localStorage.getItem('save')].selected = true;
        changeCSS();
    }
}