JS获取子节点
childNodes 访问当前节点下所有的子节点
firstChild 访问子节点中的首位
lastChild 访问子节点中的最后一位
parentNode 访问当前节点的父节点
nextSibling 访问当前节点兄弟节点中的下个节点
perviousSibling 访问当前节点兄弟节点中的上个节点
【注】上述这些属性都包含文本节点
【注】下述这些方法值获取字节点中的元素节点
children
firstElementChild
lastElementChild
nextElementSibling
previousElementSibling
nodeType nodeName nodeValue
元素节点 1 标签名 null
属性节点 2 属性名 属性值
文本节点 3 #text 文本内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function() {
var oDiv = document.getElementById("div1");
alert(oDiv.childNodes[0].nodeType);
alert(oDiv.childNodes[0].nodeName);
alert(oDiv.childNodes[0].nodeValue);
alert(oDiv.childNodes[1].nodeType);
alert(oDiv.childNodes[1].nodeName);
alert(oDiv.childNodes[1].nodeValue);
alert(oDiv.childNodes[2].nodeType);
alert(oDiv.childNodes[2].nodeName);
alert(oDiv.childNodes[2].nodeValue);
}
</script>
</head>
<body>
<div id="div1">div文本<em>em文本</em><strong>strong文本</strong></div>
</body>
</html>