HTML DOM removeNamedItem() 方法


HTML DOM removeNamedItem() 方法使用其名稱從 HTML 文件中的屬性節點刪除其引數中指定的一個節點。

語法

以下是語法 -

node.removeNamedItem(node);

示例

讓我們來看一個 removeNamedItem() 方法的示例 -

 即時演示

<!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:60%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM removeNamedItem() 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 using removeNamedItem() method</button>
<script>
   function remove() {
      var p=document.querySelector("p");
      p.attributes.removeNamedItem("style");
   }
</script>
</body>
</html>

輸出

將生成以下輸出 -

點選“使用 removeNamedItem() 方法刪除屬性”按鈕可從 <p> 元素刪除 style 屬性。

更新於:2019-07-30

81 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始吧
廣告