搜索
您的当前位置:首页正文

js对table的操作[1]

来源:六九路网


示例 html代码:

123
456

一、从页面点击表单元素,获取其他相关表单信息

//从页面点击表单元素,获取其他相关表单信息

function mouseOver(){

var lObj;

if(event){

lObj=event.srcElement;

while(lObj && lObj.tagName!=\"INPUT\") lObj=lObj.parentNode;

}

var aa =lObj.id;

alert(aa);

}

二、以某个对象为参照,获取子对象

//得到table2的id

alert(document.getElementById(\"table1\").rows[0].cells[0].childNodes[0].id);

//获取内容

alert(document.getElementById(\"table1\").rows[0].cells[0].childNodes[0].rows[0].cells[0].innerText);

alert(document.getElementById(\"table1\").rows[0].cells[0].childNodes[0].rows[0].cells[0].innerHTML);

//可以通过childNodes获取

alert(document.getElementById(\"table1\").childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerText );

//获得td1行号

alert(document.getElementById(\"table1\").rows[0].cells[0].childNodes[0].rows[0].cells[0].cellIndex);

//获得tr1行号

alert(document.getElementById(\"table1\").rows[0].cells[0].childNodes[0].rows[0].rowIndex);

三、以某个对象为参照,获取父对象

//获取父对象,以input1为例:

//td1

alert(document.getElementById(\"input1\").parentNode.id);

//tr1

alert(document.getElementById(\"input1\").parentNode.parentNode.id);

//table2

alert(document.getElementById(\"input1\").parentNode.parentNode.parentNode.parentNode.id);

//获取tr2

alert(document.getElementById(\"input1\").parentNode.parentNode.parentNode.parentNode.childNodes[0].childNodes[1].id);

//获取456

alert(document.getElementById(\"input1\").parentNode.parentNode.parentNode.parentNode.childNodes[0].rows[1].cells[0].innerText);

四、注意

//注意获取父对象以tr1例:

关系有2层父关系(不理解)

//alert(document.getElementById(\"tr1\").parentNode.parentNode.id);

//parentNode :W3C DOM使用

//parentElement DHTML DOM使用

因篇幅问题不能全部显示,请点此查看更多更全内容

Top