因此,我有一個照片庫包含照片的縮略圖和一個大的圖像。當用戶點擊縮略圖時,它應該顯示較大的縮略圖。一切都完成了,但我無法讓JS工作。我知道這很小,所以有人可以看看我做錯了嗎?先謝謝你。在orde中的可點擊事件顯示圖像
<html>
<head>
<title></title>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale = 1">
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<link rel = "stylesheet" href = "html5reset.css">
<link rel = "stylesheet" href = "gallery.css">
</head>
<body>
<div class = "main">
<div class = "wrapper">
<div id = "largeImage">
<img src = "images/machine_1.jpg" alt = "machining image" width = "920" height = "400" id = "bigImage" class = "border"/>
</div>
<div class = "thumbnail">
<img src="images/machine_1.jpg" alt = "machining lathe" id="machine_1"/>
<img src="images/machine_2.jpg" alt = "machining lathe" id="machine_2"/>
<img src="images/machine_3.jpg" alt = "machining lathe" id="machine_3"/>
<img src="images/machine_4.jpg" alt = "machining lathe" id="machine_4"/>
<img src="images/machine_5.jpg" alt = "machining lathe" id="machine_5"/>
<img src="images/machine_6.jpg" alt = "machining lathe" id="machine_6"/>
</div>
</div>
</div>
<script src = "gallery.js"></script>
</body>
</html>
function imgFunction() {
var bigImage = document.getElementById("bigImage");
var thumbnail = document.getElementById("thumbnail");
thumbnail.addEventListener("click",function(event) {
if (event.target.tagName == "IMG") {
bigImage.src = event.target.src;
}
},false);
}
window.addEventListener("load", imgFunction, false);
你得到一個錯誤?你是否完成了'console.log(event)'或'console.log(event.target)'以確保你獲得了正確的元素?你做了什麼來嘗試和調試呢? –
你沒有'id =「thumbnail」'元素,所以'document.getElementById(「thumbnail」)'不會解決任何問題。 –