HTML DOM removeAttributeNode() 方法

htmlweb developmentfront end technology更新于 2025/8/20 2:52:17

HTML DOM removeAttributeNode() 方法从 HTML 文档中的指定元素中删除其参数指定的属性,并将删除的属性作为 Attr Node 对象返回。

语法

以下是语法 −

node.removeAttributeNode(attributeNode);

示例

让我们看一个 removeAttributeNode() 方法的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   html{
      height:100%;
   }
   body{
      text-align:center;
      color:#fff;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
      height:100%;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM removeAttributeNode() method Demo</h1>
<p style="color:#db133a;font-size:1.2rem;">Hi! I'm a para element in HTML with some random text</p>
<button onclick="remove()" class="btn">Remove Attribute</button>
<script>
   function remove() {
      var p=document.querySelector("p");
      var pAtt=p.getAttributeNode("style");
      p.removeAttributeNode(pAtt);
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"删除属性"按钮从 <p> 元素中删除样式属性。


相关文章


有用资源