質問編集履歴
1
ファイルの読み込み方法について試したことを追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -60,4 +60,45 @@
|
|
60
60
|

|
61
61
|
### 試したこと
|
62
62
|
|
63
|
-
ファイルの読み込み方法を調べました。
|
63
|
+
ファイルの読み込み方法を調べました。
|
64
|
+
|
65
|
+
[追記]
|
66
|
+
現在、textareaの1つの部分にtextファイルの内容を表示させるというのができました。
|
67
|
+
複数のtextareaに分けて表示させる方法が知りたいです。
|
68
|
+
```JavaScript
|
69
|
+
<!DOCTYPE html>
|
70
|
+
<html lang="ja">
|
71
|
+
<head>
|
72
|
+
<meta charset="UTF-8">
|
73
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
74
|
+
<title>Document</title>
|
75
|
+
</head>
|
76
|
+
|
77
|
+
<body>
|
78
|
+
<p>.txtファイルを選択してください</p>
|
79
|
+
<input type="file" id="sample"><br><br>
|
80
|
+
<textarea id="text" rows="10" cols="30" readonly></textarea>
|
81
|
+
</body>
|
82
|
+
<script>
|
83
|
+
const sample = document.getElementById("sample");
|
84
|
+
const text = document.getElementById("text")
|
85
|
+
|
86
|
+
//ダイアログでファイルが選択された時
|
87
|
+
sample.addEventListener("change", function (event) {
|
88
|
+
|
89
|
+
const file = event.target.files;
|
90
|
+
|
91
|
+
//FileReaderの作成
|
92
|
+
const reader = new FileReader();
|
93
|
+
//テキスト形式で読み込む
|
94
|
+
reader.readAsText(file[0]);
|
95
|
+
|
96
|
+
//読込終了後の処理
|
97
|
+
reader.onload = function () {
|
98
|
+
//テキストエリアに表示する
|
99
|
+
text.value = reader.result;
|
100
|
+
}
|
101
|
+
});
|
102
|
+
</script>
|
103
|
+
</html>
|
104
|
+
```
|