|
<html>
<head><title>Test</title>
<script type="text/javascript">
function nodecheck(parentNode,childNode)
{
var childs=parentNode.childNodes;
for(var i=0;i<childs.length;i++)
{
if(childs[i]==childNode)
return true;
if(childs[i].hasChildNodes())
nodecheck(childs[i],childNode);
}
return false;
}
var testfun=function()
{
var e1=document.getElementById('ROOT');
var e2=document.getElementById('A');
alert(nodecheck(e1,e2));
}
</script>
</head>
<body onload="testfun();">
<div id="ROOT">
<div id="A">
<div id="AA">
<div id="AAA">
Hello
</div>
</div>
</div>
<div id="B">
<p id="BA">
<input id="BAA" type="text" />
</p>
<p id="BB">
<input id="BBA" type="button" value="lala" />
</p>
</div>
<div id="C">
<select id="CA">
<option id="CAA">1</option>
<option id="CAB">2</option>
<option id="CAC">3</option>
</select>
</div>
</div>
</body>
</html>
================================================= ============
Purpose: Enter a node A, and then enter a node B, to determine whether node B is a child node of node A
Yes return true, no return false
var e1=document.getElementById('ROOT');
var e2=document.getElementById('A');
Return true A is a child node of ROOT
var e1=document.getElementById('ROOT');
var e2=document.getElementById('AA');
Why return false?
Is this comparison judging that there is a problem with childs[i]==childNode? |
|