1

選択リストに基づいて、ユーザーのクリックによって行われた最新のスタイルの選択を保存しようとしています。そのために localstorage を使用しようとしましたが、それを機能させるのに非常に苦労しています。

どんな助けでも大歓迎です。ありがとう!

これは、いくつかの例を見て思いついた最高のものですが、うまくいきません。

function initiate(){
    var styleButton = document.getElementById("selectStyleButton");
    if (styleButton){
        styleButton.addEventListener("click", saveStyle);
    }
    setStyle();
}

function saveStyle(){
    var select = document.getElementById("selectStyle");
    if (select){
        select[select.selectedIndex].value = localStorage.setItem("selectStyle"); //store value that was selected
    }
    setStyle(); //set the style that was selected based on the above
}

function setStyle(){
    var newstyle = localStorage.getItem("selectStyle"); //get value that was selected
    document.body.className = newstyle; //change body based on the class name of value that was selected
    }

window.addEventListener("load", initiate);
4

1 に答える 1