我是 javascript 和整个网络编程的新手。我正在尝试制作将pic0.png
根据var lvl
' 值更改图像的脚本。
这是脚本:
<!DOCTYPE html>
<html>
<body>
<script>
var pic = "pic0.png";
</script>
<img id="myImg" src="pic0.png" width="107" height="98">
<p>Click the button to change the value of the src attribute of the image.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var lvl = 2;
if (lvl = 1)
{
pic = "pic1.png";
}
else if (lvl = 2)
{
pic = "pic2.jpg";
}
document.getElementById("myImg").src = pic;
}
</script>
</body>
</html>
当var lvl
等于 2 时,图片必须变为,但是有问题 - 当我点击按钮“试试看!”后,无论等于 2 还是 1 pic2.jpg
,图片都会变为。pic1.png
var lvl