日期:2014-05-16  浏览次数:20333 次

如何得到DIV 中的id值
div中有table

table 中又有table1

table1中有text框和button按钮等;

-----------------------------

鼠标点击text框和button时候,怎样得到div的id值??

------解决方案--------------------
this.parentElement.parentElement.parentElement.parentEleme.parentElement.id
中间有多少个parentElement自己算下,text的parentElement是td,再上就是tr,再上table1 以此推
可以打this.parentElement.tagName看下对象的标签
如果这个DIV中没有其它的div,可以用递归算
------解决方案--------------------
经测试,完全符合要求!请不要吝啬给分,谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
</style>
<title>无标题文档</title>
</head>
<body>
<div id="result">
<table>
<table>
<input type="text" id="start1" onclick="alert(document.getElementById('start1').parentNode.parentNode.parentNode.id)" />
<input type="button" id="start2" onclick="alert(document.getElementById('start2').parentNode.parentNode.parentNode.id)" />
</table>
</table>
</div>
</body>
</html>