@GISer Liu
问题
我自己写了一个网站,但是它没法远程储存数据,要如何通过Javascript内置的远程储存数据的工具储存数据呢?
代码
index.html
<!DOCTYPE html>
<html lang="zh">
<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 type="text/css">
.div{
width: 400px;
height: 250px;
background-color: #E1E9EF;
border-radius: 10px;
font-size: 18px;
padding: 120px 100px;
margin: 120px auto;
}
.password_submit{
height: 40px;
width: 300px;
border: none;
background-color: #F8F9F9;
padding: 0 35px;
font-size: 15px;
border-radius: 5px;
box-shadow: 0px 1px 1px rgba(255, 255, 255, 0.7), inset 0px 2px 5px #aaaaaa;
border-radius: 5px;
color: saddlebrown;
display: inline-block;
font-size: 20px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color:#fff;
background-color: rgb(16, 185, 214);
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
margin-top: 20px;
}
.password_submit:hover{
background-color: #1795bb;
}
.password_submit:active{
background-color: #1795bb;
box-shadow: 0 5px #666;
transform:translateY(4px);
}
body{
background: url(img23.jpg);
background-repeat:no-repeat;
background-size:100%;
}
</style>
</head>
<body>
<div class="div">
<h1>执勤队后台登录</h1>
<p>请输入登录密码</p>
<input type="password" class="password" placeholder="请输入密码" id="password">
<br>
<input type="submit" class="password_submit" value="登录" onclick="PassWord()">
</div>
<script type="text/javascript">
function PassWord()
{
password = document.getElementById('password').value;
JSON.parse(password);
if(password == "123456"){
window.location.href="files/start.html";
}else{
alert("密码错误");
}
}
</script>
</body>
</html>
files
chakan.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>查看队员</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>查看队员</h1>
<h2>以下是执勤队全部队员</h2>
<br>
<button onclick="ChaKan()" id="button">点击此处查看</button>
<table class="table" cellspacing="1">
<thead>
<tr><th>班级</th><th>姓名</th><th>状态</th><th>导师班级</th><th>导师姓名</th></tr>
</thead>
<tbody id="tbody"></tbody>
</table>
<script src="index.js">
console.log(localStorage.getItem("date"));
</script>
<a href="javascript:history.back(-1)">
<button id="button">返回</button> <!--返回键-->
</a>
</body>
</html>
index.css
#button{
display: inline-block;
font-size: 20px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color:#fff;
background-color: rgb(16, 185, 214);
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
margin-top: 20px;
margin-left: 550px;
}
#button:hover{
background-color: #1795bb;
}
#button:active{
background-color: #1795bb;
box-shadow: 0 5px #666;
transform:translateY(4px);
}
table, th, td {
border: 1px solid #fff;
}
table {
width: 50%;
margin-top: 20px;
margin-left: 300px;
color: #fff;
}
h1,h2,.input,p{
margin-left: 500px;
}
body{
background-color: rgb(105, 112, 0);
}
h1,h2,p{
color: #fff;
}
index.js
/*
JavaScript函数
*/
// 加入执勤队
var dates = [];
if (localStorage.getItem("data")) {
dates = JSON.parse(localStorage.getItem("data")); // 把json数据转为js对象
} else {
localStorage.setItem("data", JSON.stringify(dates));
}
function JiaRu() {
var jiaru_class = document.getElementById('jiaru_class').value;
var jiaru_name = document.getElementById('jiaru_name').value;
dates.push({
class: jiaru_class,
name: jiaru_name,
state: "未实习",
daoshi_class: null,
daoshi_name: null
});
localStorage.data = JSON.stringify(dates);
alert('加入成功!');
}
// 添加实习
function ShiXi() {
var shixi_duiyuan_class = document.getElementById('shixi_duiyuan_class').value;
var shixi_duiyuan_name = document.getElementById('shixi_duiyuan_name').value;
var shixi_daoshi_class = document.getElementById('shixi_daoshi_class').value;
var shixi_daoshi_name = document.getElementById('shixi_daoshi_name').value;
var index = dates.findIndex(item =>
item.class === shixi_duiyuan_class &&
item.name === shixi_duiyuan_name &&
item.state === "未实习"
);
if (index !== -1) {
dates[index] = {
class: shixi_duiyuan_class,
name: shixi_duiyuan_name,
state: "未转正",
daoshi_class: shixi_daoshi_class,
daoshi_name: shixi_daoshi_name
};
localStorage.data = JSON.stringify(dates);
alert('修改成功!');
} else {
alert('未找到对应记录!');
}
}
// 转正队员
function ZhuangZheng() {
var zhuangzheng_daoshi_class = document.getElementById('zhuangzheng_daoshi_class').value;
var zhuangzheng_daoshi_name = document.getElementById('zhuangzheng_daoshi_name').value;
var zhuangzheng_duiyuan_class = document.getElementById('zhuangzheng_duiyuan_class').value;
var zhuangzheng_duiyuan_name = document.getElementById('zhuangzheng_duiyuan_name').value;
var index = dates.findIndex(item =>
item.class === zhuangzheng_duiyuan_class &&
item.name === zhuangzheng_duiyuan_name &&
item.state === "未转正"
);
if (index !== -1) {
dates[index] = {
class: zhuangzheng_duiyuan_class,
name: zhuangzheng_duiyuan_name,
state: "已转正",
daoshi_class: null,
daoshi_name: null
};
localStorage.data = JSON.stringify(dates);
alert('修改成功!');
} else {
alert('未找到对应记录!');
}
}
// 踢出队员
function TiChu() {
var tichu_class = document.getElementById('tichu_class').value;
var tichu_name = document.getElementById('tichu_name').value;
var index = dates.findIndex(item =>
item.class === tichu_class &&
item.name === tichu_name
);
if (index !== -1) {
dates.splice(index, 1);
localStorage.data = JSON.stringify(dates);
alert('删除成功!');
} else {
alert('未找到对应记录!');
}
}
// 查看队员
function ChaKan() {
var str = "";
dates.forEach(function(item) {
str += "<tr><td>" + item.class + "</td><td>" + item.name + "</td><td>" + item.state + "</td><td>" + item.daoshi_class + "</td><td>" + item.daoshi_name + "</td></tr>";
});
document.getElementById('tbody').innerHTML = str; // 假设有一个 id 为 tbody 的元素
}
jiaru.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>加入执勤队</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>加入执勤队</h1>
<h2>请填写以下信息</h2>
<p>班级:</p><input type="text" placeholder="请填写班级" id="jiaru_class" class="input">
<p>姓名:</p><input type="text" placeholder="请填写姓名" id="jiaru_name" class="input">
<br>
<script src="index.js"></script>
<input type="submit" onclick="JiaRu()" id="button">
<br>
<a href="javascript:history.back(-1)">
<button id="button">返回</button> <!--返回键-->
</a>
</body>
</html>
shixi.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>新增实习</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>新增实习</h1>
<h2>请填写以下信息</h2>
<p>实习队员班级:</p><input type="text" id="shixi_duiyuan_class" placeholder="请输入实习班级" class="input">
<p>实习队员姓名:</p><input type="text" id="shixi_duiyuan_name" placeholder="请输入实习姓名" class="input">
<br>
<p>导师班级:</p><input type="text" id="shixi_daoshi_class" placeholder="请输入导师班级" class="input">
<p>导师姓名:</p><input type="text" id="shixi_daoshi_name" placeholder="请输入导师姓名" class="input">
<br>
<script src="index.js"></script>
<input type="submit" onclick="ShiXi()" id="button">
<br>
<a href="javascript:history.back(-1)">
<button id="button">返回</button> <!--返回键-->
</a>
</body>
</html>
start.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>执勤队录入</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>执勤队录入</h1>
<a href="jiaru.html">
<button id="button">加入执勤队</button>
</a>
<br>
<a href="shixi.html">
<button id="button">新增实习</button>
</a>
<br>
<a href="zhuangzheng.html">
<button id="button">新增转正</button>
</a>
<br>
<a href="tichu.html">
<button id="button">踢出队员</button>
</a>
<br>
<a href="chakan.html">
<button id="button">查看队员</button>
</a>
</body>
</html>
tichu.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>踢出队员</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>踢出队员</h1>
<h2>请输入以下信息</h2>
<p>班级:</p><input type="text" placeholder="请填写班级" id="tichu_class" class="input">
<p>姓名:</p><input type="text" placeholder="请填写姓名" id="tichu_name" class="input">
<br>
<script src="index.js"></script>
<input type="submit" onclick="TiChu()" id="button">
<br>
<a href="javascript:history.back(-1)">
<button id="button">返回</button> <!--返回键-->
</a>
</body>
</html>
zhuangzheng.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>新增转正</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1>新增转正</h1>
<h2>请输入以下信息</h2>
<p>导师班级:</p><input type="text" placeholder="请输入导师班级" id="zhuangzheng_daoshi_class" class="input">
<br>
<p>导师姓名:</p><input type="text" placeholder="请输入导师姓名" id="zhuangzheng_daoshi_name" class="input">
<br>
<p>实习班级:</p><input type="text" placeholder="请输入实习班级" id="zhuangzheng_duiyuan_class" class="input">
<br>
<p>实习姓名:</p><input type="text" placeholder="请输入实习姓名" id="zhuangzheng_duiyuan_name" class="input">
<br>
<script src="index.js"></script>
<input type="submit" onclick="ZhuangZheng()" id="button">
<br>
<a href="javascript:history.back(-1)">
<button id="button">返回</button> <!--返回键-->
</a>
</body>
</html>
@GlSer Liu