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 | | 编辑|
举报| 收藏