<html>
<head>
<script type="text/javascript" src="sample0301.js"></script>
</head>
<body>
<h1 id="hello">Hello World</h1>
</body>
</html>
以下の JavaScript により、 "Hello World" の文字が "Good Morning" に書き換わります。
const element = document.getElementById('hello');
element.innerText = "Good Morning";
以下のようにすると、 HTML タグも差し込むことが出来ます。
const element = document.getElementById('hello');
element.innerHTML = "<FONT COLOR="red">Good Morning</FONT>";
html
├ head
└ body
└ h1
└ "Hello World"
HTML が木構造で表されているという性質を使い、 JavaScript から HTML を操作できる DOM という便利な概念があります。
以下の JavaScript で、 IMG タグの src 属性のみを書き換えることが出来ます。
<html>
<head>
<script type="text/javascript" src="sample0303.js"></script>
</head>
<body>
<img id="dog" src="https://x.gd/9Uow0"></img>
</body>
</html>
const element = document.getElementById('dog');
element.setAttribute('src', 'https://x.gd/LLx5A');
$theme: gaia template: invert