我正在尝试制作一个包含4张图片的简单图库
我写了一些代码但结果总是undefined
我想要的是默认显示div中第一张带有类little
这是HTML代码:
<img class="imageToShow" />
<div class="little">
<img class="imageNotToShow" src="../Images/gallery1.jpg" />
<img class="imageNotToShow" src="../Images/gallery2.jpg" />
<img class="imageNotToShow" src="../Images/gallery3.jpg" />
<img class="imageNotToShow" src="../Images/gallery4.jpg" />
</div>
这是js代码:
var firstImageSrc;
firstImageSrc = $('.imageNotToShow').first().attr("src");
alert(firstImageSRC);
$('.ImageToShow').attr("src", firstImageSrc);
答案 0 :(得分:1)
Jquery选择器区分大小写,因此请更正类选择器 ImageToShow to imageToShow ...
var firstImageSrc = $('.imageNotToShow').first().attr("src");
$('.imageToShow').attr("src", firstImageSrc);
&#13;
.imageNotToShow{
display:none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="imageToShow" />
<div class="little">
<img class="imageNotToShow" src="http://via.placeholder.com/350x151" />
<img class="imageNotToShow" src="http://via.placeholder.com/350x152" />
<img class="imageNotToShow" src="http://via.placeholder.com/350x153" />
<img class="imageNotToShow" src="http://via.placeholder.com/350x154" />
</div>
&#13;
答案 1 :(得分:0)
$('。ImageToShow')。attr(“src”,firstImageSrc);
jQuery代码中的CSS选择器不匹配。将ImageToShow
更改为imageToShow
,一切正常。