parentNode,offsetParent

元素.parentNode : 父节点   只读 属性 当前节点的父级节点 没有兼容性问题 可放心使用

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function() {
    var aA = document.getElementsByTagName('a');
    for (var i=0; i<aA.length; i++) {
        aA[i].onclick = function() {
            this.parentNode.style.display = 'none';
        }
        
    }
    
    
}
</script>
</head>

<body>
<ul id="ul1">
  <li>11111 <a href="javascript:;">隐藏</a></li>
  <li>22222 <a href="javascript:;">隐藏</a></li>
  <li>33333 <a href="javascript:;">隐藏</a></li>
  <li>44444 <a href="javascript:;">隐藏</a></li>
</ul>
</body>
</html>

 

元素.offsetParent : 只读 属性

如果没有定位父级,默认是body

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
div {padding: 40px 50px;}
#div1 {background: red;}
#div2 {background: green;}
#div3 {background: orange;}
</style>
<script>
window.onload = function() {
    var oDiv3 = document.getElementById('div3');
    alert( oDiv3.parentNode.id );//弹出div2
    alert( oDiv3.offsetParent.id );//弹出body1
}
</script>
</head>

<body id="body1">
    <div id="div1">
        <div id="div2">
            <div id="div3"></div>
        </div>
    </div>
</body>
</html>

 


如果有定位父级则弹出父级  若定位多个父级   则弹出离当前元素最近的一个有定位属性的父节点

 

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
div {padding: 40px 50px;}
#div1 {background: red; position: relative}
#div2 {background: green;position: relative}
#div3 {background: orange;}
</style>
<script>
window.onload = function() {
    var oDiv3 = document.getElementById('div3');
    
    alert( oDiv3.offsetParent.id );//弹出div2
 } </script> </head> <body id="body1"> <div id="div1"> <div id="div2"> <div id="div3"></div> </div> </div> </body> </html> </html>

 


ie7以下,如果当前元素没有定位默认是body,如果有定位则是html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
div {padding: 40px 50px;}
#div1 {background: red; }
#div2 {background: green;}
#div3 {background: orange;position: relative}
</style>
<script>
window.onload = function() {
    var oDiv3 = document.getElementById('div3');
    //alert( oDiv3.parentNode.id );//弹出div2
    alert( oDiv3.offsetParent.tagName );//ie7弹出html IE8以上弹出body
}
</script>
</head>

<body id="body1">
    <div id="div1">
        <div id="div2">
            <div id="div3"></div>
        </div>
    </div>
</body>
</html>
</html>

 

ie7以下,如果当前元素的某个父级触发了layout,那么offsetParent就会被指向到这个触发了layout特性的父节点上

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
div {padding: 40px 50px;}
#div1 {background: red; zoom: 1;}
#div2 {background: green;}
#div3 {background: orange;}
</style>
<script>
window.onload = function() {
    
    var oDiv3 = document.getElementById('div3');
    alert( oDiv3.offsetParent.id );//ie7以下弹出的是div1,其他的弹出是BODY1
}
</script>
</head>

<body id="body1">
    <div id="div1">
        <div id="div2">
            <div id="div3"></div>
        </div>
    </div>
</body>
</html>

 





 

posted @ 2016-05-11 18:00  Cutelady  阅读(159)  评论(0)    收藏  举报