遍历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;   
    } 
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值