javascript递归问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 20:16:53
各位大虾帮忙看一下这段代码,为什么if()中的条件为假也执行里面的语句呢。

代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
<script type="text/javascript">
function getParent(elem)
{
elem=elem.parentNode;
if(elem.className!="bb")
{
getParent(elem);
//alert("ok");
}
return elem;
}
function test(obj)
{

//看一下改过后的代码,也许你能明白此中的调用关系
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
<script type="text/javascript">
function getParent(elem)
{
elem=elem.parentNode;
if(elem.className!="bb")
{
getParent(elem);
alert(elem.name);
}
return elem;
}
function test(obj)
{
var text = getParent(obj);
alert("at last "+text.nodeName);
}

</script><