源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <div id="test"> <p>点击按钮获取 div 最近的祖先元素:</p> <p><button onclick="myFunction()">点我</button></p> <p>offsetParent 为: <span id="demo"></span></p> </div> <script> function myFunction() { var testDiv = document.getElementById("test"); document.getElementById("demo").innerHTML = testDiv.offsetParent; } </script> </body> </html>
运行结果: