如何刪除所有樣式屬性,使用jQuery?


要使用jQuery從每個標籤中刪除一個屬性,可以使用removeAttr()方法和通用選擇器。讓我們看看如何使用該方法刪除所有樣式屬性。使用通用選擇器也可以選擇所有元素。

你可以嘗試執行以下程式碼來學習如何使用jQuery刪除所有樣式屬性−

範例

現場演示

<!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>

更新於: 2020年2月13日

8K + 瀏覽

開啟你的 職業生涯

完成課程,獲得認證

立即開始
廣告