<!DOCTYPE html>
<html lang="en" >
<head>
<title>demo</title>
</head>
<body >
<div >
<div id="test1"> </div>
<div></div>
</div>
<script type="text/javascript">
var getParents=function(id){
var dom=id.parentNode;
while(dom.tagName!=null){
document.write(dom.tagName);
dom=dom.parentNode;
}
return dom;
}
getParents(test1);
</script>
</body>
</html>
通过id名可以直接获取父节点
最新推荐文章于 2024-06-25 15:27:27 发布