原理:利用if语句,鼠标点击换图片,再点击在换图片。
具体图片代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="./images/dark.jpg" alt="" width="200px" id="img">
<script>
var oImg = document.getElementById("img");
//找不到判断条件的情况下,自己创造条件
var bian = true; //true---黑 false--亮
oImg.onclick = function(){
if(bian == true ){
oImg.src = "./images/bright.jpg"
bian = false;//状态要保持同步
}else{
oImg.src = "./images/dark.jpg"
bian = true;//状态要保持同步
}
}
</script>
</body>
</html>