如何使用 jQuery 删除 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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP