todolist实现刷新关闭页面内容不变,下次打开时上次的内容仍然存在

todolist

今天做了一个Vue的小程序

分享一下

先看一下效果图:

实现效果

点击刷新

内容不变,关闭效果也一样

HTML代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<style type="text/css">
		html,body{
			width:100%;
			height: 100%;
			margin: 0;
			padding: 0;
			background-color: gray;
		}
		.main{
			width:100%;
			height: auto;
		}		
		.add{
			width:100%;
			height: 2rem;
			background-color: black;
			display: flex;
			align-items: center;
			}
		.addmodel{
			width:30%;
			height: 70%;
			display: flex;
			flex-flow: row;
			justify-content: center;
			align-items: center;
			margin: 0 auto;
		}
		.add input{
			display: block;
			width:66%;
			height: 100%;
			background-color: whitesmoke;
			margin: 0 auto;
			outline: none;
			border: none;
			border-radius: 0.3rem;
			padding-left: 0.625rem;
		}
		.add button{
			display: block;
			width:3rem;
			height: 100%;
			border-radius: 0.3rem;
			border: none;
			outline: none;
		}
		.thingsmodel{
			width:40%;
			height: auto;
			display: flex;
			flex-flow: column;
			align-items: center;
			justify-content: center;
			margin: 0 auto;
		}
		.thingstitle{
			width:100%;
			height: auto;
			display: flex;
			flex-flow: row;
			justify-content: space-between;
		}
		.things{
			width:100%;
			height: auto;
		}
		.things li{
			width:100%;
			height: auto;
			display: flex;
			flex-flow: row;
			justify-content: space-between;
			align-items: center;
			font-size: 1.5rem;
			background-color: whitesmoke;
			padding: 0 1rem;
			border-left: 0.3125rem solid #bfbfbf;
		}
		.left{
			width:auto;
			display: flex;
			flex-flow: row;
			justify-content: left;
			align-items: center;
		}
		.left input{
			width:20px;
			height: 20px;
		}
		.jian{
			width:1.25rem;
			height: 1.25rem;
			border-radius: 50%;
			border: 3px double #bfbfbf;
			background-color: black;
			text-align: center;
			line-height: 1.25rem;
			color: white;
			cursor: pointer;
		}
		[v-cloak] {
		display: none;
		} 
		
	</style>
	<body>
		<div class="main">
			<div class="add">
				<p class="addmodel">
				<input type="text" v-model="msg" v-on:keyup.enter="getvalue"/>
				<button v-on:click="getvalue">添加</button>
				</p>
			</div>
			<div class="thingsmodel" v-cloak>
			<div class="thingstitle">
				<h1>正在进行</h1>
				<h1 class="index0" v-text="nowindex">0</h1>
			</div>
				<div class="things" v-if="istrue">
					<li v-for="(item,index) in nowel" class="now">
					<p class="left">
					<input name="Checkbox" type="button" v-on:click="istrue(this,item,index)">
					<span class="cont">{{item}}</span>
					</p>
					<span class="jian" v-on:click="clearnow(item,index)">-</span>
					</li>
				</div>
			</div>
			<div class="thingsmodel" v-cloak>
			<div class="thingstitle">
				<h1>已经完成</h1>
				<h1 class="index0" v-text="haventindex"></h1>
			</div>
			<div class="things" v-if="ishavent" >
				<li v-for="(item,index) in haventel" class="havent">
				<p class="left">
				<img src="./img/checked.PNG" v-on:click="ishavent(this,item,index)" />
				<span class="cont">{{item}}</span>
				</p>
				<span class="jian" v-on:click="clearhavent(item,index)">-</span>
				</li>
			</div>
			</div>
		</div>
	</body>
</html>

js代码:

var app = new Vue({
		el:'.main',
		data:{
			msg:'',
		    nowindex:0,
			nowel:[],
			haventindex:0,
			haventel:[],
			checked:true
		},
		created:function(){
			var lastnowel = JSON.parse(localStorage.getItem("nowel"));
			var nowindex = parseInt(localStorage.getItem('nowindex'));
			if(nowindex=='NAN'||typeof(nowindex)=='NAN'){
				console.log(nowindex);
				nowindex = 0;
			}
			console.log(lastnowel);
			var lasthaventel = JSON.parse(localStorage.getItem("haventel"));
			var haventindex = parseInt(localStorage.getItem("haventindex"));
			if(haventindex=='NAN'||typeof(haventindex)=='NAN'){
				haventindex=0;
			}
			var str = typeof(lastnowel);
			var str1 = typeof(lasthaventel);
			if(str!=undefined&&lastnowel!="undefined"){
				this.nowel = lastnowel;
				if(str1!=undefined&&lasthaventel!="undefined"){
					this.haventel = lasthaventel;
				}
				this.nowindex = nowindex;
				this.haventindex = haventindex;
			}
			
		},
		methods:{
			getvalue:function(){
				var msg = this.msg;
				if(msg==""||msg==null){
					alert("此字段不能为空");
					return;
				}
				this.nowindex = this.nowindex+1;
				var nowindex = this.nowindex;
				this.nowel.splice(this.nowindex-1,1,msg);
				this.localstore();
			},
			istrue:function(obj,item,index){
			this.nowel.splice(index,1)
			var len = this.haventel.length; 
			this.haventel.splice(this.haventindex,1,item);
			this.nowindex = this.nowindex-1;
			this.haventindex = this.haventindex+1;
			this.localstore();
			},
			ishavent:function(obj,item,index){
				this.haventel.splice(index,1);
				var len = this.nowel.length; 
				this.nowel.splice(this.nowindex,1,item);
				this.haventindex = this.haventindex-1;
				this.nowindex = this.nowindex+1;
				this.localstore();
				Vue.set(obj,"checked","checked");
			},
			clearnow:function(item,index){
				this.nowel.splice(index,1);
				this.nowindex = this.nowindex-1;
				this.localstore();
			},
			clearhavent:function(item,index){
				this.haventel.splice(index,1);
				this.haventindex = this.haventindex-1;
				this.localstore();
			},
			localstore:function(){
				var nowel = this.nowel;
				console.log(nowel);
				var haventel = this.haventel;
						if(typeof(nowel)!=undefined&&nowel!="undefined"&&nowel!=undefined){
							localStorage.setItem('nowel',JSON.stringify(this.nowel));
							localStorage.setItem('nowindex',this.nowindex);
							if(typeof(haventel)!=undefined&&nowel!="undefined"&&haventel!=undefined){
								localStorage.setItem('haventel',JSON.stringify(this.haventel));
								localStorage.setItem('haventindex',this.haventindex);
							}
							console.log("yes");
						}else{
							console.log(3);
						}
			}

		}
	
	});

源码:todolist

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前后端杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值