如何用children和firstElementChild做遍历
例 1.4
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</HEAD>
<BODY>
<!--马克-to-win:像后面的很多XML文件,结构都比较整齐,不会说孩子里面又有div容器又有textNode,毕竟谁写XML,会给自己制造那么些麻烦呢?比如
<div id="contain">
<div id="ia">aa </div>
abc
<div id="ia">a </div>
</div>,
上章的document.all[]也是返回所有的element.
-->
<div id="contain">
<div id="ia">aa</div>
<div id="ia">a </div>
<div id="ib">b</div>
</div>
<script>
var ps = document.getElementById("contain")
if(ps.children)
{
var oFirst= ps.firstElementChild;
oFirsta = ps.children[0];//children是一种很好的兼容写法
alert(oFirst===oFirsta);
var oLast = ps.lastElementChild;
oLasta = ps.children[ps.children.length-1];
alert(oLast===oLasta);
var oNext = oFirst.nextElementSibling;
var oPre = oNext.previousElementSibling;
alert(oFirst.firstChild.nodeValue);
alert(oLast.firstChild.nodeValue);
alert(oNext.firstChild.nodeValue);
alert(oPre.firstChild.nodeValue);
}
</script>
</BODY>
</HTML>
更多请见:https://blog.csdn.net/qq_43650923/article/details/102813629