日期:2014-05-16 浏览次数:20383 次
<div id="parent">
<div id="child1">
<div id="child11"></div>
</div>
<div id="child2"></div>
</div>
var parent = document.getElementById("parent");
<div id="parent">
<div id="child1">
<div id="child11"></div>
</div>
<div id="child2"></div>
</div>
<script type="text/javascript">
function getChildNode(childNodes, index) {
var node = -1;
for (var i = 0, j = childNodes.length; i < j; i++)
if (childNodes[i].nodeType == 1) {
node++;
if (node == index) return childNodes[i];
}
return false;
}
window.onload = function () {
var parent = document.getElementById("parent");
var child1 = getChildNode(parent.childNodes, 0);
alert(child1.id)
var child11 = getChildNode(child1.childNodes, 0);
alert(child11.id)
}
</script>