如何使用 JavaScript 返回文件中第一個影像的 ID?
若要返回第一個影像的 ID,請在 JavaScript 中使用 images 屬性。
示例
您可以嘗試執行以下程式碼,以返回文件中第一個影像的 ID −
<!DOCTYPE html>
<html>
<body>
<img id="image1" src="https://tutorialspoint.tw/html5/images/html5-mini-logo.jpg">
<img id="image2" src="https://tutorialspoint.tw/hive/images/hive-mini-logo.jpg">
<img id="image3" src="https://tutorialspoint.tw/sas/images/sas-mini-logo.jpg">
<img id="image4" src="https://tutorialspoint.tw/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