JAVAScript 灯泡(开关,图片转换)
2021/11/3 17:10:18
本文主要是介绍JAVAScript 灯泡(开关,图片转换),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
方案一 单按钮(开 / 关)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修改灯泡</title>
<style>
img{
width: 200px;
height: 300px;
}
</style>
</head>
<body>
<img src="../images/dark.jpg" alt="">
<button>开灯</button>
<script>
var img = document.getElementsByTagName('img')[0];
var btn = document.getElementsByTagName('button')[0];
var tag = true;
btn.onclick = function () {
if (tag) {
img.src = '../images/bright.jpg';
btn.innerText = '关灯';
} else {
img.src = '../images/dark.jpg';
btn.innerText = '开灯';
}
tag = !tag;
}
</script>
</body>
</html>
方案二 双按钮(开 关)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>灯泡</title>
</head>
<body>
<div class="box">
<img src="../images/bark.jpg" alt="">
<button>开</button>
<button>关</button>
</div>
<script>
var box_img = document.getElementsByClassName('box')[0].getElementsByTagName("img")[0];
var box_btn = document.getElementsByClassName('box')[0].getElementsByTagName("button");
box_btn[0].onclick = function () {
box_img["src"] = "../images/light.jpg";
}
box_btn[1].onclick = function () {
box_img.src = "../images/bark.jpg";
}
</script>
</body>
</html>
这篇关于JAVAScript 灯泡(开关,图片转换)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28一步到位:购买适合 SEO 的域名全攻略
- 2024-12-27OpenFeign服务间调用学习入门
- 2024-12-27OpenFeign服务间调用学习入门
- 2024-12-27OpenFeign学习入门:轻松掌握微服务通信
- 2024-12-27OpenFeign学习入门:轻松掌握微服务间的HTTP请求
- 2024-12-27JDK17新特性学习入门:简洁教程带你轻松上手
- 2024-12-27JMeter传递token学习入门教程
- 2024-12-27JMeter压测学习入门指南
- 2024-12-27JWT单点登录学习入门指南
- 2024-12-27JWT单点登录原理学习入门