nodeType nodeName nodeValueの実例テスト
22318 ワード
nodeType nodeName nodeValueの実例テスト
注意:nodeNameは常に大文字の値を返します。たとえ元素がHtmlドキュメントに小文字であっても。
注意:nodeNameは常に大文字の値を返します。たとえ元素がHtmlドキュメントに小文字であっても。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DOM </title>
</head>
<body>
<h1 id="h1">An HTML Document</h1>
<p id="p1">
This is a <i>W3C HTML DOM</i>
document.
</p>
<p>
<input id="btnDemo1" type="button" value=" H1 Element ">
</p>
<p>
<input id="btnDemo2" type="button" value=" H1 Element ">
</p>
<p>
<input id="btnDemo3" type="button" value=" Document Element ">
</p>
<p>
<input type="button" alt=" " title=" " name="btnShowAttr" id="btnShowAttr" value=" " />
</p>
<script type="text/javascript">
function showElement(){
var element = document.getElementById("h1");//h1 <h1>
console.log('nodetype:' + element.nodeType);//nodeType=1
console.log('nodeName:' + element.nodeName);
console.log('nodeValue:' + element.nodeValue); //null
console.log('element:' + element);
}
function showText(){
var element = document.getElementById("h1");
var text = element.childNodes[0];
console.log('nodeType:' + text.nodeType); //nodeType=3
console.log('nodeValue:' + text.nodeValue); // nodeValue
text.nodeValue = text.nodeValue + "abc"; // 。
console.log('nodeName:' + text.nodeName);
console.log(text.data); //data , 。
}
function showDocument(){
console.log('nodeType:' + document.nodeType); //9
console.log('nodeName:' + document.nodeName);
console.log(document);
}
function showAttr(){
var btn = document.getElementById("btnShowAttr");// ,
console.log(btn);
var attrs = btn.attributes;
console.log(attrs);
for (var i = 0; i < attrs.length; i++) {
console.log(attrs[i].nodeType);//attribute nodeType=2
console.log(attrs[i].nodeName);
console.log(attrs[i].nodeValue);
console.log(attrs[i].name);
console.log(attrs[i].value);
}
}
function demo(){
var btnDemo1 = document.getElementById("btnDemo1");
btnDemo1.onclick = showElement; // 1 nodetype
var btnDemo2 = document.getElementById("btnDemo2");
btnDemo2.onclick = showText;
var btnDemo3 = document.getElementById("btnDemo3");
btnDemo3.onclick = showDocument;
var btnShowAttr = document.getElementById("btnShowAttr");
btnShowAttr.onclick = showAttr;
}
window.onload = demo;
</script>
</body>
</html>