W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>获取父级 HTML 元素</h1> <h2 id="01">Hello World!</h2> <h2 id="02">Hello China!</h2> <h2 id="03">Hello Shanghai!</h2> <p id="demo"></p> <script> var myParent = document.getElementById("02").parentNode; document.getElementById("demo").innerHTML = myParent.nodeName; </script> </body> </html>