请选择 进入手机版 | 继续访问电脑版

offsetParent和son.parentNode区别与联系

[复制链接]
查看1537 | 回复0 | 2020-2-26 15:10 | 显示全部楼层 |阅读模式
offsetParent和son.parentNode区别与联系
offsetParent返回的是带定位的父亲元素。
son.parentNode返回的上一级亲父亲,不管带不带定位。
代码案例:
  1.     <style>
  2.         * {
  3.             margin: 0;
  4.             padding: 0;
  5.         }
  6.         
  7.         .zidiu {
  8.             /* position: relative; */
  9.             width: 200px;
  10.             height: 200px;
  11.             background-color: pink;
  12.             margin-left: 100px;
  13.             padding: 50px 50px;
  14.         }
  15.         
  16.         .son {
  17.             width: 100px;
  18.             height: 100px;
  19.             background-color: red;
  20.         }
  21.     </style>
  22.     <div class="zidiu">
  23.         <div class="son"></div>
  24.     </div>
  25.     <script>
  26.         var div = document.querySelector('.zidiu');
  27.         var son = document.querySelector('.son');
  28.         console.log(son.offsetParent);
  29.         console.log(son.parentNode);
  30.         // console.log(div.offsetWidth);
  31.     </script>
复制代码
输出结果为:

offsetParent和son.parentNode区别与联系

offsetParent和son.parentNode区别与联系

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-3-24