<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DHTML example</title>
</head>
<body bgcolor="red">
<script>function init() {
functionlet initmyObj = document.getElementById("navigation") {;
// ... varmanipulate myObj = document.getElementById("navigation");
// ... manipulate myObj}
window.onload = }init;
window.onload = init;
</script>!--
Often the code is stored in an external file; this is done ▼
by linking the file that contains the JavaScript. ▼
This is helpful when several pages use the same script: ▼
▲ Often the code is stored in an external file; this is done
▲ by linking the file that contains the JavaScript.
<script src=" myjavascriptmy-javascript.js"></script> ▼
▲ This is helpful when several pages use the same script:
▲ <script src="myjavascript.js"></script>
</html>
</syntaxhighlight>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Using a DOM function</title>
<style>
a { background-color: #eee; }
a:hover { background: #ff0; }
#toggleMe { background: #cfc; display: none; margin: 30px 0; padding: 1em; }
</style>
</head>
<body>
<h1>Using a DOM function</h1>
<h2><a id="showhide" href="#">Show paragraph</a></h2>
<p id="toggleMetoggle-me">This is the paragraph that is only displayed on request.</p>
<p>The general flow of the document continues.</p>
<script>
function changeDisplayState(iddisplayElement, textElement) {
if (displayElement.style.display === "none" || displayElement.style.display === "") {
var d = document.getElementById('showhide'), ▼
edisplayElement.style.display = document.getElementById(id)"block";
if (etextElement.style.displayinnerHTML === 'none' || e.style.display === '')"Hide {paragraph";
} else e.style.display = 'block';{
ddisplayElement.innerHTMLstyle.display = 'Hide paragraph'"none";
}textElement.innerHTML else= {"Show paragraph";
e.style.display = 'none';
d.innerHTML = 'Show paragraph';
}
document.getElementById('showhide').addEventListener('click', function (e) { ▼
e.preventDefault();
let displayElement = changeDisplayStatedocument.getElementById('toggleMe'"toggle-me");
▲ let var dtextElement = document.getElementById( '"showhide '"),
▲ document.getElementById('showhide')textElement.addEventListener( '"click '", function (e) {
changeDisplayState(displayElement, textElement);
</script>
</body>
</html>
</syntaxhighlight>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dynamic Styles</title>
<style>
ul { display: none; }
</style>
</head>
<body>
<h1 id="firstHeaderfirst-header">Welcome to Dynamic HTML</h1>
<p><a id="clickableLinkclickable-link" href="#">Dynamic styles are a key feature of DHTML.</a></p>
<ul id="unorderedListunordered-list">
<li>Change the color, size, and typeface of text</li>
<li>Show and hide text</li>
<li>And much, much more</li>
</ul>
<p>We've only just begun!</p>
<script>
function showMe() {
document.getElementById("firstHeaderfirst-header").style.color = "#990000";
document.getElementById("unorderedListunordered-list").style.display = "block";
}
document.getElementById("clickableLinkclickable-link").addEventListener("click", function (e) {
e.preventDefault();
showMe();
});
</script>
</body>
</html>
</syntaxhighlight>
|