see an exercise on the Internet as follows:
<body>
<div class="div1">
<div class="div2">
<div class="div3">
<div class="div4"></div>
</div>
</div>
</div>
<script>
function getNode(){
var oDiv = document.getElementsByClassName("div4")[0];
var parent = findNode(oDiv);
return parent;
}
function findNode(el){
var rul = el.parentNode;
if (!el || document.documentElement === rul || el === document.documentElement )
{
return;
}else if (rul && rul.className === "div1")
{
return rul;
}else {
return findNode(rul);
}
}
var s1 = getNode();
console.log(s1.className);
</script>
</body>
Recursively finds the final parent of the child node. The above code can get a normal value; but if you put
return findNode(rul);return findNode(el);
will report an error: the maximum call stack size is exceeded
question: why is there an error?