abcde10161 2008-08-26 15:09
浏览 997
已采纳

javascript递归时出现死循环,该如何解决

function Condition(tagName,attrName,attrValue)
{
    
    this.tagName=tagName;
    this.middleStart="<tag-name name="\&quot;&quot;+tagName+&quot;\&quot;">";
    this.middleEnd="</tag-name>"
    this.child=null;
    this.childResult="";
    
}

Condition.prototype.addChild=function(childCondition)
{
    this.child=childCondition;
    this.childResult=this.child.showXml();
}

Condition.prototype.getChild=function()
{
    if(this.child==null)
    {
        return this;
    }
    else
    {
        return this.getChild();//这里是递归
    }
}

Condition.prototype.showXml=function()
{
    return this.middleStart+this.childResult+this.middleEnd;
}

function test1()
{   
    var con=new Condition("table",null,null);
    var con1=new Condition("tr",null,null);
    var con2=new Condition("td",null,null);
    var child1=con.getChild();
    child1.addChild(con1);
    var child2=con.getChild();
    child2.addChild(con2);
    var child3=con.getChild();
    alert(con.showXml());
}

 


问题补充:
这个程序的功能主要是向服务器端的xml文件写入数据,这个xml文件里面描述了html页面的元素结构,比如html文件是这样:

,则对应的此xml文件则是:

<tag-name name="table">

<tag-attribute attributename="border">1</tag-attribute>

<tag-name name="tr">

<tag-name name="td">

</tag-name>

</tag-name>

该xml文件是在服务器端读取后封装成HtmlParser框架过滤网页内容时用的条件,以上的javascript代码就是用来生成这个XML文件的,因为HTML页面的层次有很多层,需要描述任意层次的HTML节点关系
  • 写回答

6条回答 默认 最新

  • iteye_17163 2008-08-26 21:45
    关注

    楼主那样实现的话逻辑会有点问题,刚才写了一段代码,可以实现楼主所说的功能,供楼主作一个参考吧:
    [code="js"]/**标签元素类.
    _atts是由{attName:'name',attValue:'value'}形式的对象组成的数组.*/
    function Element(_tagName,_atts){
    this.tagName=_tagName;//标签名.
    this.atts=_atts||[];//属性.
    this.childNodes=[];//子标签.
    }

    /**添加子节点.
    _element:子节点,Element类的对象.*/
    Element.prototype.appendChild=function(_element){
    this.childNodes.push(_element);
    }

    /**给元素添加属性.
    _attName:属性名,_attValue:属性值.*/
    Element.prototype.addAttribute=function(_attName,_attValue){
    this.atts.push({attName:_attName,attValue:_attValue});
    }

    /**将Element对象转换为XML形式的字符串.*/
    Element.prototype.parseToXML=function(){
    var xml=[];
    xml.push("");
    for(var i=0;i xml.push(""+this.atts[i].attValue+"");
    }
    for(var i=0;i xml.push(this.childNodes[i].parseToXML());//递归遍历子节点.
    }
    xml.push("");
    return xml.join("\n");//组成字符串并返回.
    }[/code]
    调用方法如下:[code="js"]
    var body=new Element("BODY");
    var table=new Element("TABLE",[{
    attName:"border",
    attValue:"0"
    },{
    attName:"width",
    attValue:"100%"
    }]);
    var tr = new Element("TR");
    var td = new Element("TD");
    td.addAttribute("border","1px");
    td.addAttribute("gbColor","red");
    tr.appendChild(td);
    table.appendChild(tr);
    body.appendChild(table);
    alert(body.parseToXML());//输出xml字符串[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置