如何透過 JavaScript 返回文件中第一個影像的 ID?
要返回第一個影像的 id,請使用 JavaScript 中的影像屬性。
示例
可以嘗試執行以下程式碼,以返回文件中第一個影像的 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>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP