當我在任何HTML文件中使用腳本標記時遇到問題,例如我想使用HTML,CSS和JS語言來完成照片庫。然而,腳本的部分沒有做任何事情!HTML文件中的腳本標記根本不起作用
這是代碼,是否有任何語法或邏輯錯誤!
<script type="text/javascript">
function imgFunction(){
var bigImg = document.getElementById ("bigImg");
var smallImg= \t document.getElementById ("thumbnails");
smallImg.addEventListener("click", function(event){
if (event.target.tagName=="IMG"){
\t \t bigImg.src=event.target.src;
\t \t
\t }
},false);
\t
}
window.addEventListener("load",imgFunction,false);
</script>
#gallery {
width:600px;
height:500px;
margin:0 auto;
background-color:silver;
padding:10px;
}
#bigImg {
width:600px;
height:400px;
margin:0 auto;
background-color:white;
}
#bigImg img
{
width:600px;
height:400px;
}
#thumbnails {
width:590;
height:100px;
background-color:white;
margin:5px auto;
padding: 5px;
}
#thumbnails img
{
width:100px;
height:90px;
display:block;
float:left;
margin:7.6px;
}
<h1>Photo Gallery </h1>
<br>
<div id="gallery">
<div id="bigImg">
<img src="../images/reception.jpg">
</div><!-- end "bigImg" -->
<div id="thumbnails" >
<img src="../images/reception.jpg">
<img src="../images/x-ray.jpg">
<img src="../images/patient_room.jpg">
<img src="../images/gym.jpg">
<img src="../images/dental_clinic.jpg">
</div><!-- end "thumbnails" -->
</div><!-- end "gallery" -->
'bigImg.src = event.target.src;'是不是做你認爲它是。 '#bigImg'是'div',因此它沒有'src'。您需要將'event.target.src'傳遞給'' – Marcus