注册登录界面
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>Document</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<h1>新增学员</h1>
<form class="info" autocomplete="off">
姓名:<input type="text" class="uname" name="uname">
年龄:<input type="text" class="age" name="age">
性别:
<select name="gender" class="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
薪资:<input type="text" class="salary" name="salary">
就业城市:<select name="city" class="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
<option value="曹县">曹县</option>
</select>
<button class="add">录入</button>
</form>
<div class="title">
共有数据
<span>0</span>
条
</div>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>操作</th>
<th>时间</th>
</tr>
</thead>
<tbody>
<!--
<tr>
<td>1001</td>
<td>欧阳霸天</td>
<td>19</td>
<td>男</td>
<td>15000</td>
<td>上海</td>
<td>
<a href="javascript:">删除</a>
</td>
</tr>
-->
</tbody>
</table>
<!-- <script>
// 获取元素
const uname = document.querySelector('.uname')
const age = document.querySelector('.age')
const gender = document.querySelector('.gender')
const salary = document.querySelector('.salary')
const city = document.querySelector('.city')
const tbody = document.querySelector('tbody')
// 获取所有带有name属性的元素
const items = document.querySelectorAll('[name]')
// 声明空数组,增删都对数组进行操作,减少DOM操作
const arr = []
// 录入模块
const info = document.querySelector('.info')
info.addEventListener('submit', function (e) {
// 阻止默认行为 不跳转
e.preventDefault()
// 表单验证
for (let i = 0; i < items.length; i++) {
if (items[i].value === '') {
return alert('输入内容不能为空')
}
}
// 创建新的对象
const obj = {
stuId: arr.length + 1,
uname: uname.value,
age: age.value,
gender: gender.value,
salary: salary.value,
city: city.value
}
// console.log(obj);
arr.push(obj)
// 清空表单
this.reset()
render()
})
// 渲染函数
function render() {
// 先清空tbody
tbody.innerHTML = ''
for (let i = 0; i < arr.length; i++) {
const tr = document.createElement('tr')
tr.innerHTML = `
<td>${arr[i].stuId}</td>
<td>${arr[i].uname}</td>
<td>${arr[i].age}</td>
<td>${arr[i].gender}</td>
<td>${arr[i].salary}</td>
<td>${arr[i].city}</td>
<td>
<a href="javascript:" data-id=${i}>删除</a>
</td>
`// 生成自定义属性,点击删除时,获取对应的data-id属性对应数组编号,方便删除数据
tbody.appendChild(tr)
}
}
// 删除操作
// 事件委托
tbody.addEventListener('click', function (e) {
if (e.target.tagName === 'A') {
// alert(11)
// 得到对应的数组id
// console.log(e.target.dataset.id);
arr.splice(e.target.dataset.id, 1)
render()
}
})
</script> -->
<script>
const initData = [
{
stuId: 1,
uname: 'zhangsan',
age: '20',
gender: 'female',
salary: '100',
city: 'chengdu',
time: '0409'
}
]
// 本地存储
// localStorage.setItem('data', JSON.stringify(initData))
// 渲染数据
// 读取本地存储数据,转换为对象,若没有数据用空数组代替
const arr = JSON.parse(localStorage.getItem('data')) || []
console.log(arr);
// 遍历数组,根据数据生成tr,里面填充数据,追加给tbody
// map遍历数组,返回有数据的tr数组;join把map返回的数组转换为字符串;通过innerHTML赋值给tbody
const tbody = document.querySelector('tbody')
function render() {
// map遍历数组
const trArr = arr.map(function (ele, index) {
return `
<tr>
<td>${ele.stuId}</td>
<td>${ele.uname}</td>
<td>${ele.age}</td>
<td>${ele.gender}</td>
<td>${ele.salary}</td>
<td>${ele.city}</td>
<td>${ele.time}</td>
<td>
<a href="javascript:" data-id="${index}">删除</a>
</td>
</tr>
`
})
console.log(trArr)
tbody.innerHTML = trArr.join('')
// 显示共计几条数据
document.querySelector('.title span').innerHTML = arr.length
}
render()
// 新增业务
const info = document.querySelector('.info')
// 获取需要非空判断的元素
const uname = document.querySelector('.uname')
const age = document.querySelector('.age')
const salary = document.querySelector('.salary')
const gender = document.querySelector('.gender')
const city = document.querySelector('.city')
// form表单注册提交事件,阻止默认行为
info.addEventListener('submit', function (e) {
e.preventDefault()
// 非空判断
if (!uname.value || !age.value || !salary.value) {
return alert('输入内容不能为空')
}
// 添加数据,给arr数组追加对象
arr.push(
{
// 新增的数据,比现在的length多1
// 处理StuId,若有数据,现有的最后一条数据的stuId+1,若没有,设置为1
stuId: arr.length ? arr[arr.length - 1].stuId + 1 : 1,
uname: uname.value,
age: age.value,
gender: gender.value,
salary: salary.value,
city: city.value,
time: new Date().toLocaleString()
}
)
// 渲染页面,重置表单
render()
this.reset()
// 把最新的数组存储到本地
localStorage.setItem('data', JSON.stringify(arr))
})
// 删除业务(事件委托!!!)
tbody.addEventListener('click', function (e) {
if (e.target.tagName === 'A') {
// alert(11)
console.log(e.target.dataset.id);
if (confirm('您确定要删除该条数据吗')) {
arr.splice(e.target.dataset.id, 1)
render()
localStorage.setItem('data', JSON.stringify(arr))
}
}
})
</script>
</body>
</html>
css
* {
margin: 0;
padding: 0;
}
h1 {
text-align: center;
color: #333;
margin: 20px 0
}
a {
text-decoration: none;
color: #333;
}
.info {
width: 900px;
margin: 50px auto;
text-align: center;
}
.info input,
.info select {
width: 80px;
height: 27px;
border-radius: 5px;
border: 1px solid #ccc;
padding-left: 5px;
margin-right: 15px;
/* outline: solid; */
/* 整体宽度包含元素内容和边框及padding值 */
box-sizing: border-box;
}
.info button {
width: 60px;
height: 27px;
background-color: #004085;
color: #fff;
cursor: pointer;
border-radius: 5px;
}
.info .age {
width: 50px;
}
table {
margin: 0 auto;
width: 800px;
border-collapse: collapse;
color: #004085;
}
th {
padding: 10px;
background-color: #cfe5ff;
font-size: 16px;
font-weight: 400;
}
td,
th {
border: 1px solid #b8daff;
}
td {
padding: 10px;
color: #666;
text-align: center;
font-size: 16px;
}
tbody tr {
background: #fff;
}
tbody tr:hover {
background: #e1ecf8;
}
.title {
width: 900px;
margin: 10px auto;
text-align: right;
height: 50px;
line-height: 50px;
background-color: #fff;
}
to do list
html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>ToDoList—最简单的待办事项列表</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/jquery.min.js"></script>
<script src="js/todolist.js"></script>
</head>
<body>
<header>
<section>
<label for="title">ToDoList</label>
<input type="text" id="title" name="title" placeholder="添加ToDo" required="required" autocomplete="off" />
</section>
</header>
<section>
<h2>正在进行 <span id="todocount"></span></h2>
<ol id="todolist" class="demo-box">
</ol>
<h2>已经完成 <span id="donecount"></span></h2>
<ul id="donelist">
</ul>
</section>
<footer>
Copyright © 2014 todolist.cn
</footer>
</body>
</html>
js
// 刷新页面,数据不会丢失,localstorage(只能存储字符串格式,JSON.stringify;转换为对象JSON.parse)
$(function () {
// 1.按下回车将数据存储到本地
//加载页面时也要渲染一次数据
load()
// 1.1判断按下的键是否为回车键
$('#title').on('keydown', function (event) {
if (event.keyCode === 13) {
if ($(this).val() === ('')) {
alert('请输入内容')
}
else {
// 读取本地原来的数据
const local = getData()
// console.log(local);
// 把最新的数据追加给local
local.push({ title: $(this).val(), done: false })
// 再次存储local
saveData(local)
// 本地存储数据渲染加载到页面中
load()
// 清空输入框的内容
$(this).val('')
}
}
})
// 2.删除操作(删除本地存储的数据)
$('ol,ul').on('click', 'a', function () {
// 获取本地存储
const data = getData()
// 修改数据
// 自定义属性用attr获取
let index = $(this).attr('id')
data.splice(index, 1)
// 保存到本地存储
saveData(data)
// 重新渲染页面
load()
})
// 3.正在进行和已完成任务的切换
$('ol,ul').on('click', 'input', function () {
// 获取本地存储
const data = getData()
// 修改数据
let index = $(this).siblings('a').attr('id')
data[index].done = $(this).prop('checked')
// 保存到本地存储
// 根据done的true或false判断放到ul还是ol中
saveData(data)
// 重新渲染页面
load()
})
// 4.统计正在进行和已完成的个数
// 通过遍历数组时,获取数据
// 读取本地原来的数据
function getData() {
const data = localStorage.getItem('todolist')
if (data !== null) {
return JSON.parse(data)
} else {
return []
}
}
//保存本地数据
// local是局部变量,作为参数传给形参data
function saveData(data) {
localStorage.setItem('todolist', JSON.stringify(data))
}
//渲染加载操作
// 每次渲染之前,清空数据
function load() {
// 读取本地数据
const data = getData()
console.log(data);
$('ol ,ul').empty()
let todoCount = 0//未完成的个数
let doneCount = 0//已完成的个数
// 遍历
// 通过遍历得到每个元素的完成情况,将他们渲染到不同列表下
$.each(data, function (i, n) {
if (n.done) {
$('ul').prepend(`<li><input type='checkbox' checked='checked'></input>
<p>${n.title}</p><a href="javascript:" id=${i}></a></li>`)
doneCount++
} else {
$('ol').prepend(`<li><input type='checkbox'></input>
<p>${n.title}</p><a href="javascript:" id=${i}></a></li>`)
todoCount++
}
// $('ol').prepend(`<li><checkbox></checkbox>
// <p>${n.title}</p><a href="javascript:" id=${i}></a></li>`)
})
$('#todocount').text(todoCount)
$('#donecount').text(doneCount)
}
})
css
body {
margin: 0;
padding: 0;
font-size: 16px;
background: #CDCDCD;
}
header {
height: 50px;
background: #333;
background: rgba(47, 47, 47, 0.98);
}
section {
margin: 0 auto;
}
label {
float: left;
width: 100px;
line-height: 50px;
color: #DDD;
font-size: 24px;
cursor: pointer;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
header input {
float: right;
width: 60%;
height: 24px;
margin-top: 12px;
text-indent: 10px;
border-radius: 5px;
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.24), 0 1px 6px rgba(0, 0, 0, 0.45) inset;
border: none
}
input:focus {
outline-width: 0
}
h2 {
position: relative;
}
span {
position: absolute;
top: 2px;
right: 5px;
display: inline-block;
padding: 0 5px;
height: 20px;
border-radius: 20px;
background: #E6E6FA;
line-height: 22px;
text-align: center;
color: #666;
font-size: 14px;
}
ol,
ul {
padding: 0;
list-style: none;
}
li input {
position: absolute;
top: 2px;
left: 10px;
width: 22px;
height: 22px;
cursor: pointer;
}
p {
margin: 0;
}
li p input {
top: 3px;
left: 40px;
width: 70%;
height: 20px;
line-height: 14px;
text-indent: 5px;
font-size: 14px;
}
li {
height: 32px;
line-height: 32px;
background: #fff;
position: relative;
margin-bottom: 10px;
padding: 0 45px;
border-radius: 3px;
border-left: 5px solid #629A9C;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
}
ol li {
cursor: move;
}
ul li {
border-left: 5px solid #999;
opacity: 0.5;
}
li a {
position: absolute;
top: 2px;
right: 5px;
display: inline-block;
width: 14px;
height: 12px;
border-radius: 14px;
border: 6px double #FFF;
background: #CCC;
line-height: 14px;
text-align: center;
color: #FFF;
font-weight: bold;
font-size: 14px;
cursor: pointer;
}
footer {
color: #666;
font-size: 14px;
text-align: center;
}
footer a {
color: #666;
text-decoration: none;
color: #999;
}
@media screen and (max-device-width: 620px) {
section {
width: 96%;
padding: 0 2%;
}
}
@media screen and (min-width: 620px) {
section {
width: 600px;
padding: 0 10px;
}
}