Example 1)
typeof to display alert windows for each item in the shopping list.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dom test</title>
</head>
<body>
<h1> Shopping list</h1>
<p title="mustHave">Things to definitely buy..</p>
<ul id="buy">
<li>Milk </li>
<li>Cheese </li>
<li>Juice </li>
</ul>
<script type="text/javascript">
//<![CDATA[
var lis =document.getElementById("buy").getElementsByTagName("li");
for(var i=0;i<lis.length;i++)
{
alert(typeof lis[i]);
}
//]]>
</script>
</body>
</html>
예제 2 )
Let's modify attributes inside the document and show their values in an alert window.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dom test</title>
</head>
<body>
<h1> Shopping list</h1>
<p title="mustHave">Things to definitely buy..</p>
<ul id="buy">
<li>Milk </li>
<li>Cheese </li>
<li>Juice </li>
</ul>
<script type="text/javascript">
//<![CDATA[
/*
Example 1)
var lis =document.getElementById("buy").getElementsByTagName("li");
for(var i=0;i<lis.length;i++)
{
alert(typeof lis[i]);
}*/
var ps=document.getElementsByTagName("p"); //p테그에 접
alert(ps[0].getAttribute("title")); // Read the first
's title attribute value
ps[0].setAttribute("title","a list of item"); // Change the first 's title value
alert(ps[0].getAttribute("title")); // Verify the changed value
//]]>
</script>
</body>
</html>