如何使用 jQuery 從每個標籤中刪除屬性?


要使用 jQuery 從每個標籤中刪除一個屬性,可以使用 removeAttr() 方法。您可以嘗試執行以下程式碼,瞭解如何移除屬性 -

示例

即時演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("h1").removeAttr("style");
  });
});
</script>
</head>
<body>

<h1 style="color:blue">This is heading</h1>

<p>This is demo text.</p>

<h1 style="color:red">This is heading</h1>

<p>This is demo text.</p>
<button>Remove</button>
<p>Click above to remove style attribute from all h1 elements.</p>

</body>
</html>

更新於: 13-Feb-2020

276 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告