我希望在两个按钮的点击上将图片从灯泡关闭更改为灯泡。我在W3schools中看到了类似的例子,但是,我看不到代码之间的区别。请帮忙。
<!doctype html>
<html>
<body>
<script>
function light(switch){
var pi;
if(switch == 0){
pi = "pic_bulboff.gif"
}
<!--this is if the switch value is zero-->
else{
pi = "pic_bulbon.gif"
}
<!--this is if switch value is anything else including 1-->
document.getElementById('sw').src = pi;
}
</script>
<img id="sw" src="pic_bulboff.gif">
<button onclick="light(0)">Light off</button>
<button onclick="light(1)">Light on</button>
</body>
</html>
答案 0 :(得分:5)
switch
是reserved keyword,不能用作参数名称
function light(s) { //Change argument name
var pi;
if (s == 0) {
pi = "pic_bulboff.gif";
} else {
pi = "pic_bulbon.gif";
}
document.getElementById('sw').src = pi;
document.getElementById('sw').title = pi; //Just to demonstrate
}
&#13;
<img id="sw" src="pic_bulboff.gif">
<button onclick="light(0)">Light off</button>
<button onclick="light(1)">Light on</button>
&#13;