DOM Node对象属性firstChild
指定节点的第一个子节点。
语法
以下是firstChild
属性的使用语法。
nodeObject.firstChild
示例
文件:node.xml 的内容如下 -
<Company>
<Employee category = "Technical" id = "firstelement">
<FirstName>Susen</FirstName>
<LastName>Su</LastName>
<ContactNo>1584567890</ContactNo>
<Email>susen@yiibai.com</Email>
</Employee>
<Employee category = "Non-Technical">
<FirstName>Max</FirstName>
<LastName>Su</LastName>
<ContactNo>1334667898</ContactNo>
<Email>maxsu@yiibai.com</Email>
</Employee>
<Employee category = "Management">
<FirstName>Min</FirstName>
<LastName>Su</LastName>
<ContactNo>1364562350</ContactNo>
<Email>minsu@yiibai.com</Email>
</Employee>
</Company>
以下示例演示了firstChild
属性的用法,文件:firstchild.html的内容如下 -
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<body>
<script>
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/node.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
function get_firstChild(p) {
a = p.firstChild;
while (a.nodeType!=1) {
a = a.nextSibling;
}
return a;
}
x = get_firstChild(xmlDoc.getElementsByTagName("Employee")[0]);
document.write("第一个子节点是: ");
document.write(x.nodeName);
</script>
</body>
</html>
执行上面示例代码,得到以下结果 -