用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
Dimmacro的公告
文章分类
    存档

    原创  遍历Dom节点,新增对应行 收藏

    1.取得当前节点的所有子结点
       var childNodes=oSrcRow.getAttribute("domNode").childNodes;
       for(var i=0;i<childNodes.length;i++){   
                var childNode=childNodes[i];
                 addNewRowInOneTable('tb2',childNode);
       }
    2.实现addNewRowInOneTable的方法
    function addNewRowInOneTable(tableId,childNode){
       var oTable = document.getElementById(tableId);
        if(oTable.tagName != "TABLE")
           alert("Err 5002");
       var oList = oTable.children;
       var oTBODY; 
      for(var i=0;i<oList.length;i++) {
            if(oList[i].tagName == "TBODY") {
                oTBODY = oList[i];
                break; 
          }
       }
    //  var oTR = oTBODY.lastChild; // 取得这个子结点的下一行,以便用insertBefore方法插入    var oTR=getNextTR(oTBODY,childNode);
       if(!oTR){
           oTR=oTBODY.lastChild; 
          var newTR=setNewTR(oTR,childNode,oTBODY);                            oTBODY.insertAdjacentElement("beforeEnd",newTR); 
        }else{
            var newTR=setNewTR(oTR,childNode,oTBODY);  
         oTBODY.insertBefore(newTR,oTR);
       }
    }
    3.实现setNewTR方法,得到新的一行
    function setNewTR(oTR,childNode,oTBODY){
    var newTR =oTBODY.firstChild.cloneNode(true); 
       newTR.style.display="";
    newTR.firstChild.firstChild.setAttribute("value",childNode.getAttribute("prikey")); 
     //设置隐藏域的value为地区的代码
    return newTR;
    }
    4.根据当前节点得到在位置上对应的下一行,下一行位置为当前节点的下一个兄弟节点对应的行或者当前节点的父节点的下一个兄弟节点。
      function getNextTR(oTBODY,childNode){  
      var allTableRows= oTBODY.childNodes;
      var thisNode=childNode.parentNode;
    var inputvalue=null;
    var brothernode=getBrotherNode(thisNode);
    if(brothernode){ 
      inputvalue=brothernode.getAttribute("prikey");   
        for(var i=0;i<allTableRows.length;i++){        if(parseInt(allTableRows[i].firstChild.firstChild.value)==inputvalue){ 
              return allTableRows[i]; 
        }//end if
       }
    }else{ 
      //alert("dim");  
     return null;
    }
    }
    5.遍历得到兄弟节点
    function getBrotherNode(thisnode){  
     var brothernode=thisnode.nextSibling; 
      if(brothernode){  
                 return brothernode;
       } 
      else {  
             var parentnode=thisnode.parentNode;   
        if(parentnode){ 
                  return getBrotherNode(parentnode);  //否则继续找其父节点的下一个同级节点        }else{ 
                      return null;   
        } 
      }
    }

    发表于 @ 2008年04月24日 11:02:00 | 评论( loading... ) | 编辑| 举报| 收藏

    旧一篇:Orical配置的过程步骤 | 新一篇:XPath语法使用的一些心得

    • 发表评论
    • 评论内容:
    •  
    Copyright © Dimmacro
    Powered by CSDN Blog