這很簡單我不確定爲什麼我遇到問題。我試圖模仿兩張圖像之間的翻轉卡,所以點擊時,它會改變爲其他圖像。我在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