- 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 - 头元素
- 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 Draw
- HTML - 二维码
- HTML - Modernizer
- 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 setAttribute() 方法
HTML DOM 的setAttribute() 方法用于为 DOM 中指定的元素添加或更新属性。HTML 中的属性是 HTML 元素的特殊特征或属性,提供有关该元素的附加信息。
如果指定的元素不存在,该方法不会创建该元素;相反,如果在不存在的元素上调用此方法,则会抛出错误。要为元素设置属性,必须首先确保该元素存在于 DOM 中。
以下交互式示例演示了 setAttribute() 方法在不同场景中的用法:
欢迎来到 Tutorialspoint..
您来对地方学习了……
- 如果单击“添加”按钮,此方法将向第一段添加“style”属性。
- 如果单击“更新”按钮,此方法将更新第一段的现有“style”属性。
语法
以下是 HTML DOM setAttribute() 方法的语法:
element.setAttribute(attributeName, attributeValue);
参数
此方法接受如下所述的两个参数:
| 参数 | 描述 |
|---|---|
| attributeName | 要设置或更改的属性的名称。 |
| attributeValue | 要为属性定义的新值。 |
返回值
此方法不返回任何值。
示例 1:为 HTML 元素设置属性
以下程序演示了如何使用 HTML DOM setAttribute() 方法在单击<button>时将样式设置为<h2> 元素:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
</head>
<body>
<p>Click the button below to change the color and font size of the heading:</p>
<h2 id="h">Welcome</h2>
<button onclick="changeColor()">Change Color</button>
<script>
function changeColor() {
const heading = document.getElementById('h');
heading.setAttribute('style', 'color: red; font-size: 24px;');
}
</script>
</body>
</html>
示例 2:为 Div 元素设置内联样式
以下是 HTML DOM setAttribute() 方法的另一个示例。我们使用此方法通过设置属性来更改现有<div>的内联样式:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
<style>
.container {
padding: 20px;
border: 1px solid black;
margin-bottom: 10px;
color: blue;
font-size: 18px;
}
button{
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>setAttribute() Method</h2>
<h4>Setting Inline Styles</h4>
<div id="ex" class="container">This div has inline Styles. Click below to change my Style!</div>
<button onclick="setInlineStyles()">Change Inline Styles</button>
<script>
function setInlineStyles() {
const ele = document.getElementById('ex');
ele.setAttribute('style', 'color: green; font-size: 24px;');
}
</script>
</body>
</html>
示例 3:在新的元素上创建和设置属性
以下示例演示了通过创建和设置新元素上的属性来使用setAttribute() 方法。它创建一个新的 <div> 元素并将其附加到文档正文:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
<style>
.container {
padding: 20px;
background-color: #f9f99f;
border: 1px solid black;
}
button{
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>setAttribute() Method</h2>
<p>Creates and Sets Attributes on a New Element</p>
<button onclick="createAndSetAttributes()">Create New Element</button>
<script>
function createAndSetAttributes() {
const nd = document.createElement('div');
nd.setAttribute('id', 'nd');
nd.setAttribute('class', 'container');
nd.textContent="This is a newly created div.";
document.body.appendChild(nd);
}
</script>
</body>
</html>
示例 4:为 Div 元素设置 Class 属性
此示例演示了通过在单击按钮时向现有 <div> 添加类属性 (highlight) 来使用setAttribute() 方法:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
<style>
.container {
padding: 20px;
background-color: #f0f0ff;
border: 1px solid black;
margin-bottom: 10px;
}
.highlight {
background-color: yellow;
}
button{
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>setAttribute() Method</h2>
<h4>Click button to apply class Attribute</h4>
<div id="ex" class="container">This div has class "container".</div>
<button onclick="addClass()">Add Class 'highlight'</button>
<script>
function addClass() {
const ele = document.getElementById('ex');
ele.setAttribute('class','container highlight');
}
</script>
</body>
</html>
支持的浏览器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| setAttribute() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
广告




