如何使用 JavaScript 返回文档中第一个图像的 ID?
若要返回第一个图像的 ID,请在 JavaScript 中使用 images 属性。
示例
您可以尝试运行以下代码,以返回文档中第一个图像的 ID −
<!DOCTYPE html>
<html>
<body>
<img id="image1" src="https://tutorialspoint.com/html5/images/html5-mini-logo.jpg">
<img id="image2" src="https://tutorialspoint.com/hive/images/hive-mini-logo.jpg">
<img id="image3" src="https://tutorialspoint.com/sas/images/sas-mini-logo.jpg">
<img id="image4" src="https://tutorialspoint.com/maven/images/maven-mini-logo.jpg">
<script>
var val = document.images.length;
document.write("<br>Number of images in the document: "+val);
document.write("<br>The id of the first image: "+document.images[0].id);
</script>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP