回答編集履歴
1
修正
answer
CHANGED
@@ -22,4 +22,28 @@
|
|
22
22
|
</body>
|
23
23
|
```
|
24
24
|
|
25
|
-
エレメントのロードよりも[windowのロードをトリガーに](https://developer.mozilla.org/ja/docs/Web/API/GlobalEventHandlers/onload)した方が確実かもしれません。
|
25
|
+
エレメントのロードよりも[windowのロードをトリガーに](https://developer.mozilla.org/ja/docs/Web/API/GlobalEventHandlers/onload)した方が確実かもしれません。
|
26
|
+
|
27
|
+
初期値に前回選択したものが初期選択された方が良いですし、
|
28
|
+
document.getElementById("mystyle").href が何回も出てくるのはあまりよくありません。
|
29
|
+
せっかくメソッド準備したのであればそちらも使いたいですね。
|
30
|
+
|
31
|
+
|
32
|
+
上記含めて、少し処理をまとめるとこんな感じで。
|
33
|
+
```let con = document.getElementById("container");
|
34
|
+
window.onload = init;
|
35
|
+
let cssfile = null;
|
36
|
+
function init(){
|
37
|
+
if(localStorage.getItem("cssfile") !== null)
|
38
|
+
{
|
39
|
+
cssfile = localStorage.getItem("cssfile");
|
40
|
+
select = document.getElementsByTagName("select");
|
41
|
+
select[0].value = cssfile;
|
42
|
+
changeStyle(cssfile);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
function changeStyle(cssfile){
|
46
|
+
document.getElementById("mystyle").href = cssfile;
|
47
|
+
localStorage.setItem("cssfile",cssfile);
|
48
|
+
}
|
49
|
+
```
|