js结合localStorage紫萼一个简陋的记事本

图
没有写样式,感兴趣的的可以去antd或者bs上拿点样式
主要点将你输入的内容永久的存下并填充到页面,稍微难点的是获取事件并将其转化为自己想要的格式
代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			#fixed {
				position: fixed;
				top: -100%;
				background-color: blanchedalmond;
				opacity: 0.6;
				width: 100%;
				height: 100px;
				transition: all .3s;
			}

			#fixed.show {
				top: 0px;
			}

			#addBtn {
				position: fixed;
				right: 20px;
				top: 20px;
				height: 50px;
				width: 50px;
				font-size: 20px;
				line-height: 50px;
				opacity: 0.7;
			}

			#button {
				width: 20;
				height: 20px;

			}
			#list{
				position: relative;
				top: 100px;
			}
		</style>
	</head>
	<body>
		
		<button type="button" id="addBtn">+</button>
		<table id="fixed">

			<tr>
				<td>标题</td>
				<td><input type="text" id="title" /></td>
			</tr>
			<tr>
				<td>内容</td>
				<td><input type="text" id="content" /></td>
			</tr>
			<tr>
				<td colspan="2"><button type="button" id="button">录入犯罪记录</button></td>
			</tr>
		</table>
		<hr>
		<ul id="list">

		</ul>
		<script type="text/javascript">
			var titleInput = document.getElementById('title')
			var contentInput = document.getElementById('content')
			var submitBtn = document.getElementById('button')
			var listUl = document.getElementById('list')
            

			var noteList = [


			];
			var tpl = '<li>' +
				'<p>{--date--}</p>' +
				'<h1>{--title--}</h1>' +
				'<h2>{--content--}</h2>' +
				'</li>'+
				'<hr>'
				
			var render = function() {
				var html = ''

				for (var i = 0; i < noteList.length; i++) {
					var theTime = noteList[i].date
					var theDate = new Date(theTime)
					var year = theDate.getFullYear()
					var month = theDate.getMonth() + 1
					var date = theDate.getDate()
					var hours = theDate.getHours()
					var min = theDate.getMinutes()
					var s = theDate.getSeconds()
					var day = theDate.getDay()
					min = min < 10 ? '0' + min : min
					s = s < 10 ? '0' + s : s,
						hours = hours < 10 ? '0' + hours : hours
					var dateStr = year + '年' + month + '月' + date + '日 ' + hours + '时' + min + '分' + s + '秒'+'—————————————————————————————————'+'星期'+day


					html += tpl.replace('{--date--}', dateStr)
						.replace('{--title--}', noteList[i].title)
						.replace('{--content--}', noteList[i].content)
				}
				listUl.innerHTML = html
				console.log(html)
			}
			//获取需要的值
			submitBtn.addEventListener('click', function() {
				var title = titleInput.value
				var content = contentInput.value

				//验证
				if (title.length == 0 || content.length == 0) {
					
					return
				}
				//构建数据

				// var now= new Date()
				// var year = now.getFullYear()
				// var month = now.getMonth()+1
				// var date = now.getDate()
				// var hours = now.getHours()
				// var min = now.getMinutes()
				// var s = now.getSeconds()
				//    min = min <10 ? '0'+ min :min
				// s = s <10 ? '0'+ s :s,
				// hours = hours<10 ? '0'+ hours : hours

				// var dateStr = year +'-'+month+'-'+date+' '+hours+':'+min+':'+s
				var note = {
					title: title,
					content: content,
					date: new Date().getTime()
				}
				//将数据添加到noteList
				noteList.push(note)
				//将数据添加到localStorage
				localStorage.setItem('noteList', JSON.stringify(noteList))
				noteList = JSON.parse(localStorage.getItem('noteList'))

				//重新渲染
				render()
				//点击提交之后,将提交的框框隐藏
				document.getElementById('fixed').className = ''
				//将输入框清空
				titleInput.value = ''
				contentInput.value = ''
			})

			noteList = JSON.parse(localStorage.getItem('noteList'))
			if (noteList) {
				//空数组
			} else {
				noteList = []
			}
			render()


			document.getElementById('addBtn').addEventListener('click', function() {
				document.getElementById('fixed').className = 'show'
			})
			
		</script>
	</body>
</html>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的购物车代码,使用localStorage进行本地存储: ```html <!DOCTYPE html> <html> <head> <title>购物车</title> </head> <body> <h1>购物车</h1> <ul id="cart"></ul> <hr> <h2>商品列表</h2> <ul> <li>商品1 <button onclick="addToCart('商品1', 10)">加入购物车</button></li> <li>商品2 <button onclick="addToCart('商品2', 20)">加入购物车</button></li> <li>商品3 <button onclick="addToCart('商品3', 30)">加入购物车</button></li> </ul> <script> // 添加商品到购物车 function addToCart(name, price) { // 获取购物车数据 let cart = JSON.parse(localStorage.getItem('cart')) || []; // 查找是否已存在该商品 let existingItem = cart.find(item => item.name === name); if (existingItem) { // 已存在,增加数量 existingItem.quantity++; } else { // 不存在,添加新商品 cart.push({name: name, price: price, quantity: 1}); } // 保存购物车数据 localStorage.setItem('cart', JSON.stringify(cart)); // 刷新购物车展示 showCart(); } // 展示购物车 function showCart() { // 获取购物车数据 let cart = JSON.parse(localStorage.getItem('cart')) || []; // 清空展示列表 let cartList = document.getElementById('cart'); cartList.innerHTML = ''; // 遍历购物车数据,渲染列表项 cart.forEach(item => { let li = document.createElement('li'); li.innerText = `${item.name} x ${item.quantity} = ${item.price * item.quantity}`; cartList.appendChild(li); }); } // 初始化页面 showCart(); </script> </body> </html> ``` 代码中,每次点击“加入购物车”按钮时,会将商品的名称和价格添加到购物车中。购物车数据使用localStorage进行本地存储,以便在页面刷新后仍能保持之前的数据。同时,每次更新购物车数据后,会调用showCart函数重新渲染购物车展示列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值