- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与演变
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - 内嵌框架 (Iframe)
- HTML - 短语元素
- HTML - 元标签
- HTML - 类
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表格标题与说明
- HTML - 表格样式
- HTML - 表格 Colgroup
- HTML - 嵌套表格
- HTML 列表
- HTML - 列表
- HTML - 无序列表
- HTML - 有序列表
- HTML - 定义列表
- HTML 链接
- HTML - 文本链接
- HTML - 图片链接
- HTML - 邮件链接
- HTML 颜色名称与值
- HTML - 颜色名称
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表单
- HTML - 表单
- HTML - 表单属性
- HTML - 表单控件
- HTML - 输入属性
- HTML 媒体
- HTML - 视频元素
- HTML - 音频元素
- HTML - 嵌入多媒体
- HTML 头部
- HTML - Head 元素
- HTML - 添加 Favicon
- HTML - JavaScript
- HTML 布局
- HTML - 布局
- HTML - 布局元素
- HTML - 使用 CSS 进行布局
- HTML - 响应式设计
- HTML - 符号
- HTML - 表情符号
- HTML - 样式指南
- HTML 图形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 存储
- HTML - 服务器发送事件
- HTML 其他
- HTML - 文档对象模型 (DOM)
- HTML - MathML
- HTML - 微数据
- HTML - IndexedDB
- HTML - Web 消息传递
- HTML - Web CORS
- HTML - Web RTC
- HTML 演示
- HTML - 音频播放器
- HTML - 视频播放器
- HTML - 网页幻灯片
- HTML 工具
- HTML - Velocity 绘图
- HTML - 二维码
- HTML - Modernizr
- HTML - 验证
- HTML - 颜色拾取器
- HTML 参考
- HTML - 速查表
- HTML - 标签参考
- HTML - 属性参考
- HTML - 事件参考
- HTML - 字体参考
- HTML - ASCII 码
- ASCII 码表查询
- HTML - 颜色名称
- HTML - 实体
- MIME 媒体类型
- HTML - URL 编码
- 语言 ISO 代码
- HTML - 字符编码
- HTML - 已弃用标签
- HTML 资源
- HTML - 快速指南
- HTML - 有用资源
- HTML - 颜色代码生成器
- HTML - 在线编辑器
HTML - DOM 元素 removeAttribute() 方法
removeAttribute() 方法允许您删除已在 DOM 结构中 HTML 元素上设置的任何属性。
语法
element.removeAttribute(attribute_name)
参数
此方法接受如下所述的一个参数。
| 参数 | 描述 |
|---|---|
| attribute_name | 包含要删除的属性名称的字符串。 |
返回值
removeAttribute() 方法不返回任何值。
HTML DOM 元素 'removeAttribute()' 方法示例
以下是 HTML DOM 元素中 removeAttribute() 方法在不同场景下的一些示例。
删除 Class 属性
此示例演示了如何使用 removeAttribute() 方法从元素中删除 class 属性。它包含一个段落 (<p>) 元素,该元素最初具有 highlight 类。单击按钮后,此 class 属性将从段落中删除。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.highlight {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>removeAttribute() Method</h2>
<p>
Click the button to remove a class attribute!!
</p>
<p id="myPara" class="highlight">
This paragraph has a class attribute.
</p>
<button onclick="removeClassAttribute()">
Remove Class Attribute
</button>
<div id="ot"></div>
<script>
function removeClassAttribute() {
const p=document.getElementById('myPara');
p.removeAttribute('class');
document.getElementById('ot').textContent=
'Class attribute removed!';
}
</script>
</body>
</html>
删除 href 属性
此示例演示了如何使用 removeAttribute() 方法从锚点 (<a>) 元素中删除 href 属性,并检索将显示结果的 <div> 元素。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Remove href Attribute</title>
</head>
<body>
<h1>HTML DOM Element</h1>
<h2>removeAttribute() Method</h2>
<p>Click the button below to remove the href
attribute from the anchor (<a>) element.
</p>
<a href="https://tutorialspoint.com" id="myLink">
Visit tutorialspoint.com
</a><br><br>
<button onclick="removeHref()">
Remove href Attribute
</button>
<div id="output"></div>
<script>
function removeHref() {
const link=document.getElementById('myLink');
link.removeAttribute('href');
const ot=document.getElementById('output');
ot.innerHTML =
'<p>Href attribute removed successfully!</p>'
;
}
</script>
</body>
</html>
添加和删除 Class 属性
此示例演示如何使用 removeAttribute() 方法动态地向 HTML 元素添加和删除 class 属性。代码包含一个按钮;单击它时,将删除 class 属性,再次单击它会将 class 属性添加回段落。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Conditional Attribute Removal</title>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>removeAttribute() Method</h2>
<p>Click the button to remove and add a class
attribute!!
</p>
<input type="text" id="In" value="Initial value"
readonly>
<button onclick="toggleReadonly()">
Toggle Readonly Attribute
</button>
<div id="ot"></div>
<script>
function toggleReadonly() {
const input=document.getElementById('In');
if (input.hasAttribute('readonly')) {
input.removeAttribute('readonly');
document.getElementById('ot').textContent=
'Readonly attribute removed!';
} else {
input.setAttribute('readonly', true);
document.getElementById('ot').textContent=
'Readonly attribute added!';
}
}
</script>
</body>
</html>
支持的浏览器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| removeAttribute() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
广告




