如何单击链接以更改图像的src属性(仅限Javascript)



当我单击链接时,我一直在尝试显示不同的图像,但我不想再次单击该链接以恢复为原始图像。

.HTML:

    You should keep in mind that every little bit helps.The planet can't be saved from the 
<a href = "#" id = "magic" onclick = "f1(); return false;">click</a> 
of the mouse, but it certainly takes the initiative a step forward. 
    <br>
    <br>
    </div>
 <div id = "bulb">
    <center><img src = "C:UsershpDesktopon.gif" style = "width:380px;height:280px;position:relative;top:25px;border:2px solid black;"></center>
    </div>

Javascript :

<script>
function f1()
{
 document.getElementById("bulb").src = "C:UsershpDesktopoff.gif";
}
</script>

请告诉我哪里出错了? 我已经检查了我的代码 3 次,但找不到问题。没有jQuery,因为我还不知道。

使用以下代码,使用 Javascript 更改图像。

function f1(){
var a = document.getElementById("bulb");
var b = a.querySelector('img').src = "off.gif";
}

最新更新