Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

JavaScript removeAttribute()

此方法用于从元素中删除指定的属性。它不同于 removeAttributeNode()方法。 removeAttributeNode()方法删除特定的Attr对象,但 removeAttribute()方法删除具有指定名称的属性。

语法

element.removeAttribute(attributename)

参数值

属性名称:这是必需的参数,用于指定要从元素中删除的属性名称。如果该属性不存在,则该方法不会产生任何错误。
通过使用一些示例让我们理解它。

Example1

在此示例中,有两个段落元素,其ID为 para para1 属于同一类 jtp 。在这里,我们将删除这些段落元素的 class 属性。我们必须单击给定的 HTML按钮才能看到效果。
<!DOCTYPE html>
<html>
<head>
<title>
The removeAttribute Method
</title>
<style>
.jtp {
color: red;
background-color: yellow;
}
</style>
</head>
<body>
<p>
Welcome to the lidihuo.com
</p>
<p>
Example of the removeAttribute() Method
</p>
<p id = "para" class = "jtp">
This is a paragraph element.
</p>
<p id = "para1" class = "jtp">
This is second paragraph element.
</p>
<button onclick = "fun()">
Click me!
</button>
<script>
function fun() {
document.getElementById("para").removeAttribute("class");
document.getElementById("para1").removeAttribute("class");
}
</script>
</body>
</html>
输出

Welcome to the lidihuo.com

Example of the removeAttribute() Method

This is a paragraph element.

This is second paragraph element.

Example2

在此示例中,有两个div元素,其ID为 div1 div2 。我们将 style 属性应用于这些div元素。
此处,我们将删除这些div元素的 style 属性。我们必须单击给定的 HTML 按钮以查看效果。
<!DOCTYPE html>
<html>
<head>
<title>
The removeAttribute Method
</title>
<style>
.jtp {
color: red;
background-color: yellow;
}
</style>
</head>
<body>
<p>
Welcome to the lidihuo.com
</p>
<p>
Example of the removeAttribute() Method
</p>
<div id = "div1" style = "background-color: yellow; font-size: 25px; color: red; border: 2px solid red;">
This is first div element.
</div>
<br>
<div id = "div2" style = "background-color: lightblue; font-size: 25px; color: blue; border: 2px solid blue;">
This is second div element.
</div>
<br>
<button onclick = "fun()">
Click me!
</button>
<script>
function fun() {
document.getElementById("div1").removeAttribute("style");
document.getElementById("div2").removeAttribute("style");
}
</script>
</body>
</html>
输出

Welcome to the lidihuo.com

Example of the removeAttribute() Method

This is first div element.

This is second div element.

类似地,我们可以使用 removeAttribute()方法删除 target 属性, align 属性, 只读属性等等。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4