到目前为止,我正在做一个只有2张图片的简单图片库。我希望文本随图片而改变。目前按钮不起作用,我不知道为什么。
$(document).ready(function(){
var currPic = 0;
var pictures = ["sk8.jpg", "3dprntr.jpg"];
var text = ['x','y']
$("#next").click(function(){
currPic++;
$("#myImage").attr("src", pictures[currPic]);
$("#desc").html(text[currPic]);
});
$("#prev").click(function(){
currPic--;
$("#myImage").attr("src", pictures[currPic]);
$("#desc").html(text[currPic]);
})});
和相关的html
<div id="gallery">
<img src="sk8.jpg" border="0"/>
</div>
<div id="buttons">
<input type="button" value="Previous" id="prev" />
<input type="button" value="Next" id="next" />
</div>
<p id = "desc">text</p>
答案 0 :(得分:1)
对text
image
使用相同的逻辑
同时解决几个问题:
id
上缺少image
var currPic
应为var currpic
,您在其他代码中使用的是Try out this code:
:试试这个:
$(document).ready(function(){
var currpic = 0;
var pictures = ["https://gaytravel-destinations.s3.amazonaws.com/32491/united-states-beaches-and-scenery__small.jpg", "http://www.carolinasusi.com.au/cmsvltl/img/8/5/a/596dbfe80598106527a80d1ddddf6/Le-Marche%2C-Blue-skies-and-Le-Marche-scenery.jpg"];
var texts = ["new text", "other new text"];
$("#next").click(function(){
currpic++;
$("#myImage").attr("src", pictures[currpic]);
$("#desc").text(texts[currpic]);
});
$("#prev").click(function(){
currpic--;
$("#myImage").attr("src", pictures[currpic]);
$("#desc").text(texts[currpic]);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gallery">
<img id="myImage" src="https://gaytravel-destinations.s3.amazonaws.com/32491/united-states-beaches-and-scenery__small.jpg" border="0"/>
</div>
<div id="buttons">
<input type="button" value="Previous" id="prev" />
<input type="button" value="Next" id="next" />
</div>
<p id = "desc">text</p>
{{1}}