示例 html代码:
|
一、从页面点击表单元素,获取其他相关表单信息
//从页面点击表单元素,获取其他相关表单信息
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例: