HTML DOM 图像集合属性
htmlweb developmentfront end technology更新于 2025/6/23 10:20:17
HTML DOM 图像集合属性返回 HTML 文档中所有 <img> HTML 元素的集合。
语法
以下是语法 −
document.images
属性
以下是 DOM 图像集合的属性 −
| 属性 | 说明 |
|---|---|
| length | 返回 <img> 的数量HTML 文档中的 HTML 元素。 |
方法
以下是 DOM 图像集合的方法
| 方法 | 说明 |
|---|---|
| [index] | 返回指定索引的 <img>集合中的元素 |
| item(index) | 返回集合中指定索引的 <img> 元素。 |
| namedItem(id) | 返回集合中指定 id 的 <img> 元素。 |
示例
让我们看一个 DOM images 集合属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align:center;
}
.btn{
background-color:lightblue;
border:none;
height:2rem;
border-radius:50px;
width:60%;
margin:1rem auto;
display:block;
}
.show{
color:#db133a;
font-size:2rem;
font-weight:bold;
}
</style>
</head>
<body>
<h1>DOM Image Object Example</h1>
<img src="https://www.cainiaomax.com/spring/images/spring-mini-logo.jpg"
alt="Learn Spring">
<img src="https://www.cainiaomax.com/hibernate/images/hibernate-mini-logo.jpg"
alt="Learn Hibernate">
<img src="https://www.cainiaomax.com/sql/images/sql-mini-logo.jpg" alt="Learn
SQL">
<img src="https://www.cainiaomax.com/plsql/images/plsql-mini-logo.jpg" alt="Learn
PL/SQL">
<button onclick="getCount()" class="btn">Count Number Of Images</button>
<div class="show"></div>
<script>
function getCount() {
document.querySelector('.show').innerHTML=document.images.length;
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"统计图片数量"按钮获取此网页上的图片总数 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

