講者:琪雅
時間:2019/11/20
<html>
<body>
<h3>HTML DOM</h3>
<div>
<span>階層結構</span>
<span>樹狀結構</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM</title>
<script type="text/javascript">
function change(){
document.body.innerHTML = "Welcome home";
}
</script>
</head>
<body>
<button onclick="change()">Change</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM</title>
<script type="text/javascript">
function change(){
var divObject=document.getElementById("endgame");
divObject.innerHTML = "Iron Man";
}
</script>
</head>
<body>
<div>I am <span id="endgame">inevitable</span>.</div>
<button onclick="change()">Change</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM</title>
<script type="text/javascript">
function change(){
var divObject=document.getElementById("endgame");
divObject.innerHTML = "Iron Man";
divObject.style.fontWeight = "bold";
divObject.style.color = "red";
}
</script>
</head>
<body>
<div>I am <span id="endgame">inevitable</span>.</div>
<button onclick="change()">Change</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM</title>
<script type="text/javascript">
function change(){
var obj=document.createElement("div");
obj.innerHTML="這是新的 div 標籤物件";
document.body.appendChild(obj);
}
</script>
</head>
<body>
<div id="target">Hello World</div>
<button onclick="change()">Change</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM</title>
<script type="text/javascript">
function change(){
var obj=document.getElementById("target");
obj.parentNode.removeChild(obj);
}
</script>
</head>
<body>
<div id="target">Hello World</div>
<button onclick="change()">Change</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM 選單開合操作</title>
<script type="text/javascript">
function toggleMenu(){
var menu=document.getElementById("menu");
if(menu.style.display=="none"){ //目前是藏起來的狀態
menu.style.display="block";
}else{ //目前是秀出來的狀態
menu.style.display=="none";
}
}
</script>
</head>
<body>
<div onclick="toggleMenu">Marvel</div>
<ul id="menu">
<li>Iron Man</li>
<li>Thor</li>
<li>Captain America</li>
</ul>
</body>
</html>
menu.classList.toggle("hide");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM 選單開合操作</title>
<script type="text/javascript">
function toggleMenu(){
var menu=document.getElementById("menu-"+number);
menu.classList.toggle("hide"); //切換標籤物件class的hide設定
}
</script>
<style type="text/css">
.hide{display: none;}
</style>
</head>
<body>
<div onclick="toggleMenu">Marvel</div>
<ul id="menu">
<li>Iron Man</li>
<li>Thor</li>
<li>Captain America</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML DOM 選單開合操作</title>
<script type="text/javascript">
function toggleMenu(number){
var menu=document.getElementById("menu-"+number);
menu.classList.toggle("hide"); //切換標籤物件class的hide設定
}
</script>
<style type="text/css">
.hide{display: none;}
</style>
</head>
<body>
<div onclick="toggleMenu(1)">Marvel</div>
<ul id="menu-1" class="hide">
<li>Iron Man</li>
<li>Thor</li>
<li>Captain America</li>
</ul>
<div onclick="toggleMenu(2)">DC</div>
<ul id="menu-2" class="hide">
<li>Batman</li>
<li>Superman</li>
<li>Wonder Woman</li>
<li>Aquaman</li>
</ul>
<div onclick="toggleMenu(3)">Harry Potter</div>
<ul id="menu-3" class="hide">
<li>Harry Potter</li>
<li>Hermione Granger</li>
<li>Ron Weasley</li>
</ul>
</body>
</html>
HTML DOM - Google簡報: https://docs.google.com/presentation/d/1-XJ5NzWkM2iGVgVnelBIAIsoY01aIe63EDiaO6xEMVU/edit#slide=id.p
網頁前端工程入門:HTML DOM - 基本觀念 By 彭彭: https://www.youtube.com/watch?v=iZ3LfVujGCM&t=1023s
網頁前端工程入門:HTML DOM - 選單開合範例 By 彭彭: https://www.youtube.com/watch?v=SWhNYC6QYDc