HTML DOM ownerDocument 属性

htmlweb developmentfront end technology更新于 2025/8/29 1:16:17

HTML DOM ownerDocument 属性返回一个 Document 对象,该对象表示节点的所有者文档。

语法

以下是语法 −

node.ownerDocument

示例

让我们看一个 ownerDocument 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   html{
      height:100%;
   }
   body{
      text-align:center;
      color:#fff;
      background: #ff7f5094;
      height:100%;
   }
   p{
      font-weight:700;
      font-size:1.2rem;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
   .show{
      font-size:1.5rem;
   }
</style>
</head>
<body>
<h1>DOM ownerDocument Property Demo</h1>
<p>Who is the owner of this blue button?</p>
<button onclick="showOwner()" class="btn">Get Owner</button>
<div class="show"></div>
<script>
   function showOwner() {
      document.querySelector(".show").innerHTML=document.querySelector(".btn").ownerDocument.nodeName
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"获取所有者"按钮了解 ownerDocument 属性的工作原理。


相关文章


有用资源