这很简单我不确定为什么我遇到问题。我试图模仿两张图像之间的翻转卡,所以点击时,它会改变为其他图像。我在if/else语句中遇到了问题,因为每次单击图像时,它都不会将其显示到else部分。在HTML页面的源代码中,图像的src被更改,但每次都会传递if语句。使用Javascript:更改img src onclick只能使用一次
(function() {
// attaches event handler to image
window.onload = function() {
var image1 = document.getElementById("image1");
image1.onclick = changeImage;
};
// changes image when clicked to flip from image to text and text to image
function changeImage() {
if (document.getElementById("image1").src = "img/top.png") {
document.getElementById("image1").src = "img/toptext.png";
//window.alert('hi');
}
else {
window.alert('it passed');
document.getElementById("image1").src="img/top.png";
}
}
})();
检查比较运算在你的if语句,提示你使用的arent一个 –
的**,如果**陈述条件是问题。将等号** = **替换为等号** == **。 – Anson