网页开关灯
DOM版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> </title>
<style type="text/css">
.cls{
background-color: rgba(0,0,0,1);
}
</style>
</head>
<body>
<input type="button" value="开/关灯" id="btn">
<script type="text/javascript">
document.getElementById("btn").onclick = function () {
document.body.className = document.body.className !="cls"?"cls":"";
}
</script>
</body>
</html>
jQuery版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
.light{
background-color: black;
}
</style>
</head>
<body>
<input type="button" value="开关" id="btn"/>
<script src="jquery-1.12.1.js"></script>
<script>
$("#btn").click(function () {
if($("body").hasClass("light")){
$("body").removeClass("light");
}else{
$("body").addClass("light");
}
});
</script>
</body>
</html>
另一个版本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<input type="button" value="关灯" id="btn"/>
<script src="jquery-1.12.1.js"></script>
<script>
$("#btn").click(function () {
if($(this).val()=="关灯"){
$("body").css("backgroundColor","black");
$(this).val("开灯");
}else{
$("body").css("backgroundColor","");
$(this).val("关灯");
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
.cls{
background-color: black;
}
</style>
<script src="jquery-1.12.1.js"></script>
<script>
$(function () {
$("#btn").click(function () {
$("body").toggleClass("cls");
});
});
</script>
</head>
<body>
<input type="button" value="开/关" id="btn"/>
</body>
</html>