日期:2014-05-19  浏览次数:20869 次

怎样获取父div 的 ID?
在线等待,感谢各位大虾

------解决方案--------------------

<div id= "x ">
sdsdddsdd
<div onclick= "GetID(this) "> 单击得到id </div>
dd </div>
<script>
function GetID(o)
{
e = o.parentNode
while(e.nodeType != 1)
{
alert(e.nodeType)
e = e.parentNode
}
alert(e.getAttribute( "id "))
}
</script>

Firefox不支持parentElement


------解决方案--------------------
<!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>
<title> new document </title>
<script type= "text/javascript ">
<!--
function GetDivID(dv)
{
e = dv.parentNode
while(e.nodeType != 1)
{
e = e.parentNode
}
return e.getAttribute( "id ");
}

//-->
</script>
</head>

<body>
<div id= "divParent ">
父层.............
<div onclick= "alert(GetDivID(this)); " style= "cursor: hand; "> 单击得到id </div>
</div>
</body>
</html>


nodeType Property Internet Development Index

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

Retrieves the type of the requested node.

What 's New for Microsoft&reg; Internet Explorer 6
This property now applies to the attribute object.

Syntax

HTML N/A
Scripting [ iType = ] object.nodeType

Possible Values

iType Integer that receives one of the following values.1 Element node.
3 Text node.


The property is read-only. The property has no default value.