2016-11-14 18 views
-1

2つのボタンがあり、これらのボタンをクリックするとスタイルシートを変更できます。しかし、一度リフレッシュしたり、自分のウェブサイトの次のページに行くと、スタイルシートは元の状態に戻ってしまい、ユーザーの設定方法は変わりません。私は同様の例を探していましたが、クッキーを使用しているものしか見つからなかったので、localStorageを使ってこれをどうすればできるのか知りたかったのです。 ありがとうございます...localStorageを使用してスタイルシートを保持

<script> 
    function swapStyleSheet(sheet) { 
    document.getElementById('pagestyle').setAttribute('href', sheet); 
    } 
</script> 

<div class="change"> 
    <h6>Change Stylesheet</h6> 
    <button onclick="swapStyleSheet('css/style2.css')">Dark</button> 
    <button onclick="swapStyleSheet('css/style.css')">Light</button> 
</div> 
+0

確かに - あなたはその部分を書いてみるべきです。 –

答えて

0

はい。ありがとうございます。以下を使用できます:

localStorage.setItem('currentStyleSheet', 'css/style.css'); 
function swapStyleSheet(sheet) { 
    document.getElementById('pagestyle').setAttribute('href', sheet); 
    localStorage.setItem('currentStyleSheet', sheet); 
} 
window.onload = function() { 
    document.getElementById('pagestyle').setAttribute('href', localStorage.getItem('currentStyleSheet')); 
} 
+0

お返事ありがとうございます。私はそれを元のスクリプトに置き換え、ボタンはまだ動作しますが、ページをリロードするとスタイルは保持されません... – 100kasimpasali

関連する問題