标签:blank java targe round bsp net document head https
假设元素没有指定位置,近期的元素或者根元素(标准模式下是html,怪异模式下是body)就是offsetParent。
当元素的style.display=‘none‘时。offsetParent返回null。
因为offsetTop和offsetLeft是相对于页边距,因此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>
标签:blank java targe round bsp net document head https
原文地址:http://www.cnblogs.com/gavanwanggw/p/6882081.html