2016-05-16 83 views
-3

當我在任何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" -->

+1

'bigImg.src = event.target.src;'是不是做你認爲它是。 '#bigImg'是'div',因此它沒有'src'。您需要將'event.target.src'傳遞給'' – Marcus

回答

1

有兩個問題。

第一:你不能把<script>標籤裏面的一個腳本。

第二:您正在設置bigImg.src但您有<div id="bigImg">

你把id放在錯誤的元素上,所以你試圖設置div(這是胡說)的src而不是圖像。

0

您的腳本僅引用bigImg這是一個Div,因此您不能爲其設置src。相反,你需要進去bigImgIMG標籤:

function imgFunction(){ 
 
    console.log("inside"); 
 
var bigImg = document.getElementById ("bigImg"); 
 
var smallImg= \t document.getElementById ("thumbnails"); 
 
     smallImg.addEventListener("click", function(event){ 
 
     console.log(event.target.tagName) 
 
     if (event.target.tagName=="IMG"){ 
 
     console.log(event.target.src); 
 
     var image = document.querySelector('#bigImg img'); 
 
\t \t image.src=event.target.src; 
 
\t \t 
 
\t } 
 
},false); 
 
\t 
 
} 
 
window.addEventListener("load",imgFunction,false);
#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" -->

0

要設置srcdiv而不是img

變化dividbigImgContainer或任何其他與設置的imgidbigImg

工作片段:

#gallery { 
 
width:600px; 
 
height:500px; 
 
margin:0 auto; 
 
background-color:silver; 
 
padding:10px; 
 
} 
 

 

 
#bigImgContainer { 
 
width:600px; 
 
height:400px; 
 
margin:0 auto; 
 
background-color:white; 
 

 
} 
 

 
#bigImgContainer 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="bigImgContainer"> 
 
<img id="bigImg" src="http://placehold.it/150?text=Img1"> 
 
</div><!-- end "bigImg" --> 
 

 
<div id="thumbnails" > 
 
<img src="http://placehold.it/150?text=Img1"> 
 
<img src="http://placehold.it/150?text=Img2"> 
 
<img src="http://placehold.it/150?text=Img3"> 
 
<img src="http://placehold.it/150?text=Img4"> 
 
<img src="http://placehold.it/150?text=Img5"> 
 

 
</div><!-- end "thumbnails" --> 
 

 

 
</div><!-- end "gallery" --> 
 

 
<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>

0

在這裏,你試圖設置SRC上該div,這是不正確的。 你需要在img上設置。

<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"){ 
 
     document.querySelector("#bigImg img").src=event.target.src; 
 
\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" -->