一、介绍

  todolist,即待办事项。在windows android ios上参考微软家出的那个To-Do应用,大概就是那样的。我这个更简单,功能只有“待办” “已完成”两项,并且是在浏览器打开的。

二、界面和文件结构这些...

  实际在浏览器中的网页如下: 利用前端三大件(html+css+js)开发一个简单的“todolist”项目-LMLPHP

  在subline中的文件结构有index.html、index.css、index.js各一个,如下图:

利用前端三大件(html+css+js)开发一个简单的“todolist”项目-LMLPHP

三、程序

  参考注释即可看懂。

(1)index.html文件

 <!DOCTYPE html>
<html>
<head>
<!-- http-equiv指定文档的内容类型和编码类型 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- name属性 视图和描述 name+content -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>ToDoList—最简单的待办事项列表</title>
<meta name="description" content="ToDoList无须注册即可使用,数据存储在用户浏览器的html5本地数据库里,是最简单最安全的待办事项列表应用!" />
<!-- 外接式css -->
<link rel="stylesheet" href="./index.css">
</head>
<body>
<!-- 头部:在这添加任务 -->
<div class="header">
<div class="box">
<form action="javascript:postaction()" id="form">
<!-- for将label标签绑定到input -->
<label for="title">ToDoList</label>
<!-- required规定提交表单之前有必填字段 autocomplete:自动补齐-->
<input type="text" id="title" name="title" placeholder="添加ToDo" required="required" autocomplete="off" />
</form>
</div>
</div>
<!-- 主体:在这进行任务和已完成 -->
<div class="content">
<h2 onclick="save()">正在进行 <span id="todocount"></span></h2>
<ol id="todolist" class="demo-box">
</ol>
<h2>已经完成 <span id="donecount"></span></h2>
<ul id="donelist">
</ul>
</div>
<!-- 脚部:印记和全清除按钮 -->
<div class="footer">
Copyright &copy; 2018 todolist.cn <a href="javascript:clear();">clear</a>
</div>
<!-- 外接式js -->
<script type="text/javascript" src="./index.js"></script>
</body>
</html>

index.html

(2)index.css文件

 /*清除默认样式  并设置简单样式*/
body {
margin:;
padding:;
font-size: 16px;
background: #CDCDCD;
} .header {
height: 50px;
background: #333;
/*background: rgba(47,47,47,0.98);*/
} .header .box,.content{
width: 700px;
padding: 0 10px;
margin: 0 auto;
}
/*.content{
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;
/*首行缩进10px*/
text-indent: 10px;
/*圆角边框 好看不止一点点*/
border-radius: 5px;
/*盒子阴影 inset内阴影*/
box-shadow: 0 1px 0 rgba(255,255,255,0.24), 0 1px 6px rgba(0,0,0,0.45) inset;
border: none
}
/*选中输入框 轮廓的宽度为0*/
input:focus {
outline-width:;
}
/*父相子绝*/
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标签的默认样式*/
ol,ul {
padding:;
list-style: none;
} 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);
}
/*任务单选框*/
li input {
position: absolute;
top: 2px;
left: 10px;
width: 22px;
height: 22px;
cursor: pointer;
}
/*任务内容*/
p {
margin:;
}
/*任务内容的文本输入框,用来修改里面的内容*/
li p input {
top: 3px;
left: 40px;
width: 70%;
height: 20px;
line-height: 14px;
text-indent: 5px;
font-size: 14px;
} ul li {
border-left: 5px solid #999;
/*不透明度 0完全透明~1完全不透明*/
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;
}

index.css

(3)index.js文件

 function clear() {
localStorage.clear();
load();
} function postaction() {
// 获取title节点
var title = document.getElementById("title");
if (title.value.trim() == "") {
alert("内容不能为空");
} else {
var data = loadData();
var todo = { "title": title.value, "done": false };
data.push(todo);
saveData(data);
var form = document.getElementById("form");
form.reset();
load();
}
} function loadData() {
var collection = localStorage.getItem("todo");
if (collection != null) {
return JSON.parse(collection);
} else return [];
} function saveSort() {
var todolist = document.getElementById("todolist");
var donelist = document.getElementById("donelist");
var ts = todolist.getElementsByTagName("p");
var ds = donelist.getElementsByTagName("p");
var data = [];
for (i = 0; i < ts.length; i++) {
var todo = { "title": ts[i].innerHTML, "done": false };
data.unshift(todo);
}
for (i = 0; i < ds.length; i++) {
var todo = { "title": ds[i].innerHTML, "done": true };
data.unshift(todo);
}
saveData(data);
} function saveData(data) {
localStorage.setItem("todo", JSON.stringify(data));
} function remove(i) {
var data = loadData();
var todo = data.splice(i, 1)[0];
saveData(data);
load();
} function update(i, field, value) {
var data = loadData();
var todo = data.splice(i, 1)[0];
todo[field] = value;
data.splice(i, 0, todo);
saveData(data);
load();
} function edit(i) {
load();
var p = document.getElementById("p-" + i);
title = p.innerHTML;
p.innerHTML = "<input id='input-" + i + "' value='" + title + "' />";
var input = document.getElementById("input-" + i);
input.setSelectionRange(0, input.value.length);
input.focus();
input.onblur = function() {
if (input.value.length == 0) {
p.innerHTML = title;
alert("内容不能为空");
} else {
update(i, "title", input.value);
}
};
} function load() {
var todolist = document.getElementById("todolist");
var donelist = document.getElementById("donelist");
var collection = localStorage.getItem("todo");
if (collection != null) {
var data = JSON.parse(collection);
var todoCount = 0;
var doneCount = 0;
var todoString = "";
var doneString = "";
for (var i = data.length - 1; i >= 0; i--) {
if (data[i].done) {
doneString += "<li draggable='true'><input type='checkbox' onchange='update(" + i + ",\"done\",false)' checked='checked' />" +
"<p id='p-" + i + "' onclick='edit(" + i + ")'>" + data[i].title + "</p>" +
"<a href='javascript:remove(" + i + ")'>-</a></li>";
doneCount++;
} else {
todoString += "<li draggable='true'><input type='checkbox' onchange='update(" + i + ",\"done\",true)' />" +
"<p id='p-" + i + "' onclick='edit(" + i + ")'>" + data[i].title + "</p>" +
"<a href='javascript:remove(" + i + ")'>-</a></li>";
todoCount++;
}
};
todocount.innerHTML = todoCount;
todolist.innerHTML = todoString;
donecount.innerHTML = doneCount;
donelist.innerHTML = doneString;
} else {
todocount.innerHTML = 0;
todolist.innerHTML = "";
donecount.innerHTML = 0;
donelist.innerHTML = "";
}
} window.onload = load; // window.addEventListener("storage", load, false);

index.js

  

05-11 11:12
查看更多