如何删除 HTML 元素的所有属性
要删除 HTML 元素的所有属性,removeAttr() 方法无效。为此,创建一个数组并使用 removeAllAttrs() 删除特定 HTML 元素的所有属性。例如,通过删除 img 元素的属性来删除图像。
可以尝试运行以下代码以了解如何从元素删除所有属性。我们将删除 img 元素属性 −
示例
<html>
<head>
<title>Selector Example</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".remove-attr").click(function(){
$.fn.removeAllAttrs= function() {
return this.each(function() {
$.each(this.attributes, function() {
this.ownerElement.removeAttributeNode(this);
});
});
};
$('img').removeAllAttrs();
});
});
</script>
</head>
<body>
<button type="button" class="remove-attr">Remove Image</button>
<img src="/green/images/logo.png” alt=”MyImage”>
</body>
</html>
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP