我想在我的網頁中交換兩個imgs,並且無法包裝我的頭爲什麼此代碼不起作用。任何幫助,將不勝感激。用一個函數(JavaScript)交換一個img src
這是在HTML文件中:
<img onclick="swap(); "src="LaptopThin.jpg" id="LaptopPicture" />
,而這是在一個單獨的JavaScript文件:
function swap()
{
var picture = document.getElementById('LaptopPicture').src;
if (picture === 'LaptopThin.jpg') {
picture.src = 'ServerRack.jpg';
} else {
picture.src = 'LaptopThin.jpg';
}
我改變了代碼:
function swap()
{
var picture = document.getElementById('LaptopPicture');
if (picture.src === 'LaptopThin.jpg') {
picture.src = 'ServerRack.jpg';
} else {
picture.src = 'LaptopThin.jpg';
}
}
和圖片在html文件中仍然沒有改變。在IMG停留在Laptopthin.jpg
的輸出是什麼,如果你在控制檯打印 「圖片」 ..? –