js原生offsetParent解析
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <title>offsetParent属性</title> <style type="text/css"> div { margin: 5px; } #div11 { border: 2px solid #0000ff; } #div21 { position: relative; border: 2px solid #ffff00; } #div30 { position: relative; border: 2px solid red; } #div31 { } </style> <script type="text/javascript"> function initPage() { getOffsetParent('div12'); //BODY getOffsetParent('div22');//DIV getOffsetParent('div32');//BODY } function getOffsetParent(id) { var divEl = document.getElementById(id); var el = divEl.offsetParent; console.log(el.nodeName); console.log('element id is ' + el.getAttribute('id') || ''); } </script> </head> <body onload="initPage()"> <div id="div11"> <div id="div12">测试1</div> </div> <div id="div21"> <div id="div22">测试2</div> </div> <div id="div30"> <div id="div31"> <div id="div32">测试3</div> </div> </div> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。