我有一个2部分的问题
第1部分:在下面的html文件中我有一个切换开关,想要保存状态,所以当我返回页面时它的位置就是我离开它的方式
更新: 第2页现在正处于保存状态 Page2.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>page 2</title>
<style>
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: green;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: red;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
<script>
function save(){
var checkbox = document.getElementById('stus');
localStorage.setItem('stus', checkbox.checked);
}
function load(){
var checked = JSON.parse(localStorage.getItem('stus'));
document.getElementById("stus").checked = checked;
}
function del(){
location.reload();
localStorage.clear()
}
load();
</script>
<body onload="load()"> <input type="button" id="ReserveerButton1" value="save" onclick="save()"/>
<input type="button" id="Wisbutton1" value="delete" onclick="del()"/>
<h2>status</h2>
<label class="switch">
<input type="checkbox" id="stus">
<div class="slider round"></div>
</label>
<label class="switch">
<input type="checkbox">
<div class="slider round"></div>
</label>
</body>
</html>
第2部分:现在page2.html正在保存已保存的状态,我可以在page1.html上获得相同的状态
第1.html页
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Project</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('#stat').load('bw14.html' '#status')
}
</script>
</head>
<body>
<a href="page2.html" >Page2</a><label>Status: </label><div id="stat"></div>
</body>
</html>
答案 0 :(得分:0)
要记录复选框状态,您必须查看Cookie或本地存储空间。
要获取复选框的状态:
$('.switch input').is(":checked")