jQuery TodoList 案例

这是一个使用HTML、CSS和jQuery编写的简单待办事项列表应用。用户可以通过输入框添加任务,回车后任务会显示在正在进行的任务列表中。已完成的任务可以被标记并移至已完成后列表。应用还提供了删除任务的功能,并且支持通过本地存储持久化数据。页面布局适应不同设备屏幕大小,具有良好的交互体验。

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="index.css">
    <script src="../jQuery-code/jQuery.js"></script>
    <script src="index.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 &copy; 2014 todolist.cn
    </footer>


</body>

</html>

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;
    }
}

JS部分:

$(function(){
	load();
	$("#title").on("keydown",function(event){              //绑定 键盘按下事件
		if(event.keyCode == 13){                           //回车的阿斯克码是13
			var local = getDate();//读取本地数据 保存在local数组中
			local.push({title: $(this).val() , done: false});//给local数组追加新的数据 
			saveDate(local);//把更新完的local数组 保存到本地存储  这样就实现了本地存储的数据更新
			load();//渲染加载
			$(this).val("");
		}
	})
	//删除操作
	$("ol,ul").on("click","a",function(){
		var data = getDate();  //获取数据
		var index = $(this).attr("id"); //获取索引号
		data.splice(index,1);//删除数据
		saveDate(data);//保存到本地
		load();//渲染页面		
	})
	//
	$("ol , ul").on("click","input",function(){
		var data = getDate();
		var index = $(this).siblings("a").attr("id");
		data[index].done = $(this).prop("checked");
		saveDate(data);
		load();
	})
	
	// 存数据的格式 var todolist = [{title:'xxxxxx' , done: false}]
	//读取本地存储数据 函数
	function getDate(){
		var data = localStorage.getItem("todolist");      //获取本地存储 叫 todolist的数据
		if(data !== null){
			return JSON.parse(data);                      //把字符串类型转化为对象类型
		}else{
			return [];
		}
	};
	
	//保存本地存储数据 函数
	function saveDate(data){
		localStorage.setItem("todolist",JSON.stringify(data));//本地存储只能 存放字符串 要转换		
	};
	
	//渲染加载函数
	function load(){
		var todoCount = 0;
		var doneCount = 0;
		var data = getDate();  //获取数据
		$("ol,ul").empty(); //清空ol,ul里的元素
		$.each(data,function(i,ele){ //遍历数据  有几个数据就生成几个li
			if(ele.done){
				var li = $("<li><input type='checkbox' checked/> <p>"+ ele.title +"</p> <a id="+ i +"></a> </li>");
				$("ul").prepend(li);
				doneCount++;
				console.log(doneCount);
			}else{
				var li = $("<li><input type='checkbox'/> <p>"+ ele.title +"</p> <a id="+ i +"></a> </li>");
				$("ol").prepend(li);
				todoCount++;
			}
		})	
		$("#todocount").text(todoCount);
		$("#donecount").text(doneCount);
	};
	
})

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值