前端-js例子:todolist

news/2024/9/26 0:42:41/

实现效果图:

 


实现步骤:

1.body部分

        1.首先,设置输入数据的框和按钮进行操作

        2.设置一个表格标签(有边框),首先在表头放置两列(“事项”‘’操作)

<body><div class="container"><div class="addBox"><input type="text" class="info"><input type="button" value="add" class="btn"></div><table border="1"><thead><tr><th>事项</th><th>操作</th></tr></thead><tbody><!-- <tr><td>晚上上课</td><td><input type="button" value="mark"><input type="button" value="update"><input type="button" value="delete"></td></tr><tr><td>晚上上课</td><td><input type="button" value="mark"><input type="button" value="update"><input type="button" value="delete"></td></tr><tr><td>晚上上课</td><td><input type="button" value="mark"><input type="button" value="update"><input type="button" value="delete"></td></tr> --></tbody></table></div>
</body>

2.css样式设置

1. 先设置外边框
css">*{padding: 0;margin: 0;}
2.设置元素的位置
css">.container{width: 19%;min-width: 200px;/* background-color: aqua; */margin: 150px auto;}
3.输入框实现横向布局
css">.container .addBox{display: flex;}
4.设置输入框输入内容部分
css">.container .addBox .info{width: 88%;border: 1px solid lightcoral;padding: 5px;/* 外轮廓去掉 */outline: none;border-radius: 5px 0 0 5px;}
5.输入框按钮设置
css">.container .addBox .btn{width: 12%;border: none;color: #fff;background-color:  lightcoral;padding: 5px;cursor: pointer;border-radius: 0 5px 5px 0;}

鼠标悬停时按钮颜色变浅

css">.container .addBox .btn:hover{opacity: 0.9;}
6.下面表格设置
css">table{margin-top: 10px;width: 100%;/* 共用一个边框 */border-collapse: collapse;}

表头设置

css">table thead tr{background-color: lightcoral;color: #fff;}table thead tr th{padding: 3px;font-size: 13px;}

表格主体部分设置

        每行颜色交错,方便观看

css">table tbody tr:nth-child(odd){background-color: antiquewhite;}table tbody tr:nth-child(even){background-color: #fff;}

        每行每个单元格的设置

css">table tbody tr td{font-size: 13px;padding: 5px;text-align: center;}table tbody tr td input{background:none;border: rgb(158, 29, 29) 1px solid;padding: 4px;color: rgb(158, 29, 29);border-radius: 4px;cursor: pointer;}table tbody tr td input:hover{box-shadow: 2px 2px 2px rgb(158, 29, 29);}table tbody tr td,table thead tr th {border-color: #fff;}

3.js变换

1.先获取元素
javascript">var btn= document.querySelector(".btn")var info=document.querySelector(".info")var tbody=document.querySelector("tbody")
2.找到要修改的行
javascript">            var updateIndex
3.表格行的起始位置值
javascript">            var rowIndex=0
4.给add按钮绑定事件
1.修改

4.1.1找到表格所有的行

4.1.2找到要修改的行(回显)

        如果表格不为空,把输入框里的值传进要修改的行里

        清空输入框里的值

        按钮改为“添加”

4.1.3结束

javascript">if(updateIndex){// 修改var trs=document.querySelectorAll("table tbody tr")console.log(trs)for(var l=0;l<trs.length;l++){if(trs[l].getAttribute("index")==updateIndex){if(info.value.trim()){trs[l].firstElementChild.innerText=info.valueinfo.value=""btn.value="add"updateIndex=undefined}else{alert("不能为空")}}}return}
javascript">// 回显var updates=document.querySelectorAll(".update")for(var i=0;i<updates.length;i++){updates[i].onclick=function(){var target=this.parentElement.previousElementSiblingif(target.style.textDecoration==""){info.value=target.innerTextbtn.value="update"updateIndex=this.parentElement.parentElement.getAttribute("index")}else{alert("事情已经完成啦")}}}

 

2.添加

4.2.1创建一行元素

4.2.2将输入框的值赋给新添加的元素

4.2.3清空输入框的值

javascript"> if(info.value.trim()){// 添加// 创建一行元素var tr=document.createElement("tr")var td_one=document.createElement("td")var td_two=document.createElement("td")// 获取输入框的值,并赋值给第一个tdtd_one.innerText=info.value// 第二个tdtd_two.innerHTML='<input type="button" value="mark" class="mark"><input type="button" value="update" class="update"><input type="button" value="delete" class="delete">'// 将td放入tr中tr.appendChild(td_one)tr.appendChild(td_two)// 设置属性tr.setAttribute("index",rowIndex)rowIndex++// 将tr放入tbody中tbody.appendChild(tr)// console.log(tr)// console.log(td_one)// console.log(td_two)// 清空输入框的值info.value=""

 

3.标记

4.3.1 找到所有的mark

4.3.2 找到点击的那一行

4.3.3对应前面的值

4.3.4 划线

javascript"> //生成之后找mark画中划线,标记var marks=document.querySelectorAll(".mark")// console.log(marks)for(var i=0;i<marks.length;i++){marks[i].onclick=function(){var target=this.parentElement.previousElementSiblingif(target.style.textDecoration==""){target.style.textDecoration="line-through"target.style.color="#888"}else{target.style.textDecoration=""target.style.color="#000"}}}

 

4.删除

4.4.1 找到所有删除行

4.4.2 找到点击的那一行

4.4.3 如果划线,进行删除(确认)

javascript"> var deletes=document.querySelectorAll(".delete")console.log(deletes)for(var j=0;j<deletes.length;j++){deletes[j].onclick=function(){var target=this.parentElement.parentElement// this表示当前点击的这个元素if(this.parentElement.previousElementSibling.style.textDecoration=="line-through"){if(confirm("确认要删除吗?")){target.parentElement.removeChild(target)}else{alert("感谢手下留情")}}else{alert("努力完成,不要半途而废")}}}

 


http://www.ppmy.cn/news/1530515.html

相关文章

ArcGIS Pro SDK (十四)地图探索 6 图形与工具

ArcGIS Pro SDK (十四)地图探索 6 图形与工具 文章目录 ArcGIS Pro SDK (十四)地图探索 6 图形与工具1 图形叠加1.1 图形叠加1.2 图形叠加与 CIMPicture图形1.3 添加带有文本的叠加图形2 工具2.1 更改草图工具的符号2.2 创建用于地图中单击的点的返回坐标的工具2.3 创建用于…

Python基于flask框架的智能停车场车位系统 数据可视化分析系统fyfc81

目录 技术栈和环境说明解决的思路具体实现截图系统设计python语言django框架介绍flask框架介绍性能/安全/负载方面可行性分析论证python-flask核心代码部分展示python-django核心代码部分展示技术路线操作可行性详细视频演示源码获取 技术栈和环境说明 结合用户的使用需求&…

uniapp map设置高度为100%后,会拉伸父容器的高度

推荐学习文档 golang应用级os框架&#xff0c;欢迎stargolang应用级os框架使用案例&#xff0c;欢迎star案例&#xff1a;基于golang开发的一款超有个性的旅游计划app经历golang实战大纲golang优秀开发常用开源库汇总想学习更多golang知识&#xff0c;这里有免费的golang学习笔…

9/24作业

1. 分文件编译 分什么要分文件编译&#xff1f; 防止主文件过大&#xff0c;不好修改&#xff0c;简化编译流程 1) 分那些文件 头文件&#xff1a;所有需要提前导入的库文件&#xff0c;函数声明 功能函数&#xff1a;所有功能函数的定义 主函数&#xff1a;main函数&…

我的AI工具箱Tauri版-VideoIntroductionClipCut视频介绍混剪

本教程基于自研的AI工具箱Tauri版进行VideoIntroductionClipCut视频介绍混剪。 本项目为自研的AI工具箱Tauri版中的视频剪辑模块&#xff0c;专注于自动生成视频介绍片段。该模块名为 VideoIntroductionClipCut&#xff0c;用户可以通过该工具快速进行视频的混剪和介绍内容的生…

linux中mysql安装教程(特别详细)

linux中mysql安装教程&#xff08;特别详细&#xff09; Linux 安装 MySQL【CentOS】 Linux 安装MySQL

Why Is Prompt Tuning for Vision-Language Models Robust to Noisy Labels?

文章汇总 本文的作者针对了提示学习的结构设计进行了分析&#xff0c;发现了一些规律&#xff1a; 1)固定的类名令牌为模型的优化提供了强正则化&#xff0c;减少了由噪声样本引起的梯度。 2)从多样化和通用的web数据中学习到的强大的预训练图像文本嵌入为图像分类提供了强大…

【RabbitMQ】幂等性、顺序性

幂等性 概述 幂等性是数学和计算机科学中某些运算的性质&#xff0c;他们可以被多次应用&#xff0c;而不会改变初始应用的结果。RabbitMQ的幂等性则是指同一条消息&#xff0c;多次消费&#xff0c;对系统的影响是相同的。 一般消息中间件的消息传输保障分为三个层级&#…