- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与演变
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - 内嵌框架
- 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 - 地理位置 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 - Web幻灯片
- 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 属性
HTML DOM 的attributes 属性允许您访问属于 HTML 元素的属性集合。此集合作为 NamedNodeMap 返回,允许您使用其名称与属性交互并检索其值。
NamedNodeMap 是一个对象,它保存元素属性的集合。它允许我们通过名称索引访问属性,并支持各种操作。
语法
以下是 HTML DOM 的attributes 属性的语法:
node.attributes
参数
由于它是一个属性,因此不接受任何参数。
返回值
此属性返回包含 HTML 元素属性集合的 'NameNodeMap',如果元素没有属性则返回 'null'
示例 1:修改 p 元素的属性
以下示例演示了 HTML DOM 的attributes 属性的用法。它修改了<p> 元素的内容:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM attributes</title>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p></p>Modifies attributes of the P Element</p>
<p id="myParagraph">Example Text</p>
<button onclick="modifyParagraph()">Modify</button>
<script>
function modifyParagraph() {
var paragraph = document.getElementById("myParagraph");
paragraph.setAttribute("title", "Modified");
paragraph.textContent = "Modified Text";
paragraph.classList.add("highlight");
}
</script>
</body>
</html>
示例 2:检查属性是否存在
以下是 HTML DOM 的attributes 属性的另一个示例。此属性用于检查段落元素上是否存在“title”属性:
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Attributes</title>
</head>
<body>
</p>Checking for attribute Existence</p>
<p id="myParagraph" title="Example Title">Example Text</p>
<button onclick="checkAttributeExistence()">Check Attribute</button>
<script>
function checkAttributeExistence() {
var paragraph = document.getElementById("myParagraph");
var hasTitle = paragraph.hasAttribute("title");
alert("Title Attribute Exists: " + hasTitle);
}
</script>
</body>
</html>
示例 3:从 HTML 元素检索属性列表
下面的示例检索并显示 HTML <div> 元素的所有属性。单击按钮时,所有属性都将以列表格式显示:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM attributes</title>
</head>
<body>
<div id="ex-Div"class="example" data-info="ex-data">
Hit the button to see the Div Elements...
</div>
<button onclick="showAttributes()">Show Attributes</button>
<ul id="attributeList"></ul>
<script>
function showAttributes() {
const element = document.getElementById('ex-Div');
const attributeList = document.getElementById('attributeList');
attributeList.innerHTML = '';
for (let attr of element.attributes) {
const li = document.createElement('li');
li.textContent = `${attr.name}:
${attr.value}`;
attributeList.appendChild(li);
}
}
</script>
</body>
</html>
示例 4:获取特定属性值
此示例从 ID 为“myParagraph”的段落元素中检索 title 属性的值,并在警报中显示它:
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM attributes</title>
</head>
<body>
<p>Getting Specific Attribute</p>
<p id="myParagraph" title="Example Title">Example Text</p>
<button onclick="getAttributeValue()">Get Attribute Value</button>
<script>
function getAttributeValue() {
var paragraph = document.getElementById("myParagraph");
var titleValue =paragraph.getAttribute("title");
alert("Title Attribute Value: " + titleValue);
}
</script>
</body>
</html>
支持的浏览器
| 属性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| attributes | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
广告




