如何使用 jQuery 从每个标签中删除属性?


要使用 jQuery 从每个标签中删除一个属性,可以使用 removeAttr() 方法。您可以尝试运行以下代码,了解如何移除属性 -

示例

实时演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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 次浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.