1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <style type="text/css"> 7 #box{ 8 /*position: relative;*/ 9 } 10 #div1{ 11 width: 100px; 12 height: 200px; 13 background: red; 14 /*position: relative;*/ 15 /*margin-top: 10px;*/ 16 } 17 #div2{ 18 width: 50px; 19 height: 100px; 20 background: green; 21 22 } 23 </style> 24 </head> 25 <body> 26 <div id="box"> 27 <div id="div1"> 28 <div id="div2"> 29 30 </div> 31 </div> 32 </div> 33 34 </body> 35 36 <script type="text/javascript"> 37 var div2 = document.getElementById("div2"); 38 39 // offsetparent 寻找的是离他最近的父节点(要求:使用了定位),否则他会获取根节点 40 41 console.log(div2.offsetParent); 42 </script> 43 </html>
时间: 2024-11-05 22:43:29