题目:
打印出 id=“bj” 该节点的所有子节点的(nodeName, nodeType, nodeValue)同时打印文本值 北京 海淀 奥运
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>yuhan20081021</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<ul>
<li id="bj" value="beijing">北京
<p>海淀</p>
奥运
</li>
<li id="sh" value="shanghai">上海</li>
</ul>
</body>
<script language="JavaScript">
//打印出 id="bj" 该节点的所有子节点的(nodeName, nodeType, nodeValue)
var bjElement=document.getElementById("bj");
alert(bjElement.firstChild.nodeName);
alert(bjElement.firstChild.nodeType);
alert(bjElement.firstChild.nodeValue);
alert(bjElement.childNodes[1].nodeName);
alert(bjElement.childNodes[1].nodeType);
alert(bjElement.childNodes[1].nodeValue);
alert(bjElement.childNodes[2].nodeName);
alert(bjElement.childNodes[2].nodeType);
alert(bjElement.childNodes[2].nodeValue);
//同时打印文本值 北京 海淀 奥运
alert(bjElement.childNodes[0].nodeValue);
alert(bjElement.childNodes[1].firstChild.nodeValue);
alert(bjElement.childNodes[2].nodeValue);
</script>
</html>
本文通过一个具体的HTML示例介绍了如何使用JavaScript来获取指定ID的元素及其所有子节点的信息,包括nodeName, nodeType和nodeValue,并展示了如何进一步操作文本节点。
1598

被折叠的 条评论
为什么被折叠?



