[英]How do i let user keep clicking on an image and change the image
This is my javascript: It basically allows users to click on the second image (thermometer) and it will change to a slightly higher temperature AND at the same time, the beaker will be changed too accordingly. 这是我的javascript:基本上,它允许用户单击第二个图像(温度计),它将更改为稍高的温度,同时,烧杯也将相应更改。 How do i let the user keep on clicking and the image to respond accordingly? 我如何让用户继续单击并相应地响应图像?
$(document).ready(function () {
$("#image").click(function () {
//when image is clicked, change image
var img2 = document.getElementById('image2');
img2.src = "images/beaker1.png";
var img = document.getElementById('image');
img.src = "images/t1.png";
return false;
});
});
Html file: HTML文件:
<div class="col-sm-6">
<img id="image2" src="images/beaker0.png" class="img-rounded">
<img src="images/saturated_percipitate_1.png" class="img-rounded">
</div>
<div class="col-sm-6">
<p><b>Increase Temperature</b></p>
<img id="image" src="images/t0.png" class="img-rounded"/>
<p><b>Decrease Temperature</b></p>
</div>
</div>
Add a class notSelected to your image 向您的图像添加一个notSelected类
<img id="image2" src="images/beaker0.png" class="img-rounded notSelected">
then try toggling this class and add src attribute depending on a certain class. 然后尝试切换该类并根据特定类添加src属性。 This should work: 这应该工作:
$("#image").on('click',function(){
$("#image2").toggleClass("selected notSelected");
// change image
$("img.selected").attr('src','images/beaker1.png');
$("img.notSelected").attr('src','images/t1.png');
});
Hope this helps. 希望这可以帮助。
If I understand you correctly, maybe this what you are looking for. 如果我正确理解您的意思,也许这就是您想要的。
The idea is to use counter for current image shown. 想法是使用计数器显示当前图像。
$(document).ready(function () {
var indexImage = 1;
$("#image").click(function () {
//check if thermometer is at max.
if(indexImage > 13){ //13 is example maximum value.
return false;
}
//when image is clicked, change image
var img2 = document.getElementById('image2');
img2.src = "images/beaker" + indexImage + ".png";
var img = document.getElementById('image');
img.src = "images/t" + indexImage + ".png";
indexImage++;
return false;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.