以下の JavaScript により、 "Hello World" の文字が "Good Morning" に書き換わります。
const element = document.getElementById('hello');
element.innerText = "Good Morning";
setTimeout 関数の使い方:
ほぼ setInterval と同じです。ただし、関数は1回しか呼ばれません。
function display() {
const element = document.getElementById('hello');
element.innerText = "Good Morning";
}
setTimeout(display, 1000);
以下のようにすると、 HTML タグも差し込むことが出来ます。
const element = document.getElementById('hello');
element.innerHTML = '<FONT COLOR="#FF0000">Good Morning</FONT>';
※タグを文字列で書き換えるよりも効率の良い方法があります。
html
├ head
└ body
└ h1
└ "Hello World"
HTML が木構造で表されているという性質を使い、 JavaScript から HTML を操作できる DOM という便利な概念があります。
document.body.innerHTML = '<h1>Good Evening</h1>';
以下の JavaScript で、 IMG タグの src 属性のみを書き換えることが出来ます。
<html>
<body>
<img id="dog" src="https://x.gd/9Uow0"></img>
</body>
<script type="text/javascript" src="sample0403.js"></script>
</html>
const element = document.getElementById('dog');
element.setAttribute('src', 'https://x.gd/LLx5A');
<html>
<body>
<div id="myid">Hello!</div>
</body>
<script type="text/javascript">
const element = document.getElementById("myid");
console.log(element.innerText);
element.innerHTML = '<span style="color:#ff0000;">Good Evening!</span>';
</script>
</html>
<html>
<body>
<div>Hello!</div>
</body>
<script type="text/javascript">
console.log(location.href);
</script>
</html>
<html>
<body>
<div>Hello!</div>
</body>
<script type="text/javascript">
location.href = "http://www.google.co.jp/";
</script>
</html>
<input type="button" value="button" id="myid2" onclick="buttonClick()">
<script type="text/javascript">
function buttonClick(){
alert('Click');
}
</script>
HTML と JavaScript ファイルは、別ファイルに分けておきたい。
function buttonClick(){
alert('Click');
}
const button = document.getElementById('myid2');
button.addEventListener('click', buttonClick);
<a href="#" id="weather" download="sample.html">ダウンロード</a>
document.getElementById('weather').addEventListener('click', (event) => {
// 保存する文字列の Blob オブジェクトを作成
const blob = new Blob(["<html><body><h1>hello</h1></body></html>"],
{type: 'text/html'});
// a 要素の href 属性に Object URL を セット
event.currentTarget.href = window.URL.createObjectURL(blob);
});
今ダウンロードしたファイルを読み出してみましょう。
ファイルの読み書きには通常 File API を使用します。
<input id="myfile" type="file">
const f = document.getElementById('myfile');
f.addEventListener('change', function(evt) {
const input = evt.target;
const file = input.files[0];
const reader = new FileReader();
reader.onload = () => {
// 読み出し結果の表示
console.log(reader.result);
};
reader.readAsText(file); // 読み込み開始
});
,」で区切ります。| タイトル | 著者 | 発行年 |
|---|---|---|
| 博士の愛した数式 | 小川 洋子 | 2003 |
| 円周率πの不思議 | 堀場 芳数 | 1989 |
| 超幾何関数入門 | 木村 弘信 | 2007 |
タイトル,著者,発行年
博士の愛した数式,小川 洋子,2003
円周率πの不思議,堀場 芳数,1989
超幾何関数入門,木村 弘信,2007
// 配列を定義
const csvArray = [];
// 改行ごとに配列化
const lines = body.split(/\r\n|\n/);
// 1行ごとに処理
for (let i = 0; i < lines.length; ++i) {
let cells = lines[i].split(",");
csvArray.push(cells);
}
console.log(csvArray);
※画面表示などで使用する制御文字エスケープシーケンスについては、 JavaScript 本格入門 P73 を参照
$theme: gaia template: invert