前端——JavaScript练习 做一个todoList

devtools/2024/9/23 21:16:17/

前端制作一个todoList的表格,实现更新、删除、修改等功能。

涉及几个知识点:

  • 设置最小高度(宽度):
.container{min-width: 350px;/* 最小宽度 最小不会小于210px */        
}
  •  去掉外轮廓
 outline: none;
  • 去除字符串两端的空白字符(包括空格、制表符、换行符等)
info.value.trim()
  •   previousElementSibling 属性返回指定元素的前一个兄弟元素(相同节点树层中的前一个元素节点)
var target=this.parentElement.previousElementSibling
  •   confirm 询问
                            
    if(confirm("是否删除该事件?")){target.parentElement.removeChild(target)
    }

    最终结果显示:

全部代码如下: 

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{padding: 0;margin: 0;}.container{width: 10%;min-width: 350px;/* 最小宽度 最小不会小于210px *//* background: blanchedalmond; */margin: 150px auto;}.container .addBox{display: flex;}.container .addBox .info{width: 80%;border: 2px solid burlywood;outline: none;/* 去掉外轮廓 */padding: 5px;cursor: pointer;border-radius: 10%;}.container .addBox .botton{width: 15%;background: rgb(157, 110, 202);color: #ffffff;padding: 5px;border-radius: 10px;}.container .addBox .botton:hover{opacity:0.8;}table{margin-top: 15px;width: 100%;border-collapse: collapse;}table thead tr{font-size: 15px;padding: 5px;background:rgb(155, 69, 192) ;color: #ffffff;}table tbody tr:nth-child(odd){background: rgb(212, 174, 198);}table tbody tr:nth-child(even){background: rgb(208, 174, 220);}table tbody tr td{padding: 5px;font-size: 15px;text-align: center;}table tbody tr td input{background: none;border: rgb(155, 69, 192) 3px solid;color: rgb(255, 255, 255);border-radius: 3px;cursor: pointer;}table tbody tr td input:hover{box-shadow: 1px 2px 1px rgb(233, 230, 216);}/* table thead tr td{text-decoration: line-through;} */</style><script>window.onload=function(){// 获取元素var botton= document.querySelector(".botton")var info= document.querySelector(".info")var tbody=document.querySelector("tbody")// 表格行的起始位置值是0var rowindex=0// 当前要修改的行的值var updateIndex// 给add按钮绑定事件botton.onclick=function(){if(updateIndex){// 修改var trs=document.querySelectorAll("table tbody tr")for(var l=0;l<trs.length;l++){if(trs[l].getAttribute("index")==updateIndex){if(info.value){trs[l].firstElementChild.innerText=info.valueinfo.value=""botton.value="add"updateIndex=undefined}else{alert("不能为空")}}}return}// console.log(info.value)if(info.value.trim()){// trim()消除值前没有用的空格// 添加//创建元素 document.createElement("标签名称")var tr=document.createElement("tr")var td_one=document.createElement("td")var td_two=document.createElement("td")// 获取输入框中的值,innerText内部文字td_one.innerText=info.value// innerHTML内部结构td_two.innerHTML='<input type="button" value="mark" class="mark"><input type="button" value="update" class="update"><input type="button" value="delete" class="del">'//td放入tr中  appendChild(元素)tr.appendChild(td_one)tr.appendChild(td_two)// 设置indextr.setAttribute("index",rowindex)rowindex++//tr放入tbody中tbody.appendChild(tr)// 清空输入框的值 给info.value赋值info.value=" "// 删除var dels=document.querySelectorAll(".del")for(j=0;j<dels.length;j++){dels[j].onclick=function(){var target=this.parentElement.parentElementif(this.parentElement.previousElementSibling.style.textDecoration=="line-through"){if(confirm("是否删除该事件?")){target.parentElement.removeChild(target)// confirm 询问}else{alert("删除已取消!")}}else{alert("坚持完成,不要半途而废")}}}// 中划线var marks=document.querySelectorAll(".mark")for(var i=0;i<marks.length;i++){marks[i].onclick=function(){var target=this.parentElement.previousElementSibling// previousElementSibling 属性返回指定元素的前一个兄弟元素(相同节点树层中的前一个元素节点)if(target.style.textDecoration==""){target.style.textDecoration="line-through"target.style.color="#888"}else{target.style.textDecoration=""target.style.color="#000"}}}// 回显var updates=document.querySelectorAll(".update")for(k=0;k<updates.length;k++){updates[k].onclick=function(){var target=this.parentElement.previousElementSiblingif(target.style.textDecoration==""){info.value=target.innerTextbotton.value="update"updateIndex=this.parentElement.parentElement.getAttribute("index")}else{alert("事件已经完成了")}}}}}}</script>
</head>
<body><div class="container"><div class="addBox"><input type="text" class="info"><input type="button" value="add" class="botton"></div><table border="1"><thead><tr><th>事项</th><th>操作</th></tr></thead></table></div></body>
</html>


http://www.ppmy.cn/devtools/116176.html

相关文章

CRC校验的生成多项式如何转换为移位寄存器

CRC校验的生成多项式转换为移位寄存器主要涉及到线性反馈移位寄存器&#xff08;LFSR&#xff09;的应用。LFSR是内测试电路中最基本的标准模块结构&#xff0c;它既可以用于产生伪随机测试码&#xff0c;也可以用于CRC校验码的生成。以下是详细的转换过程&#xff1a; 1. 理解…

专题·大模型安全 | 生成式人工智能的内容安全风险与应对策略

正如一枚硬币的两面&#xff0c;生成式人工智能大模型&#xff08;以下简称“生成式大模型”&#xff09;在助力内容生成的同时也潜藏风险&#xff0c;成为虚假信息传播、数据隐私泄露等问题的温床&#xff0c;加剧了认知域风险。与传统人工智能&#xff08;AI&#xff09;相比…

【软件测试】--xswitch将请求代理到测试桩

背景 在做软件测试的过程中&#xff0c;经常会遇见需要后端返回特定的响应数据&#xff0c;这个时候就需要用到测试桩&#xff0c;进行mock测试。 测试工程师在本地模拟后端返回数据时&#xff0c;需要将前端请求数据代理到本地&#xff0c;本文介绍xswitch插件代理请求到flas…

Flyway 基本概念

Flyway 基本概念详解 Flyway 是一款非常流行的数据库版本控制工具&#xff0c;专为管理数据库的变更而设计。它帮助开发者在项目开发过程中自动管理数据库的迁移与版本控制&#xff0c;确保数据库结构的变更和代码版本一致。Flyway 可以自动执行 SQL 脚本或 Java 代码来管理数…

ITOP-2 分模块安装部署itop

ITOP-2 分模块安装部署itop 一、安装PHP组件1、查看当前Linux服务器安装的PHP版本2、安装源epel&#xff0c;安装源remi&#xff0c;安装yum-config-manager3、用yum-config-manager指定remi的php7.2仓库4、安装升级php5、验证当前PHP的版本 二、部署 MySQL 服务1、设置 Repo2、…

学习使用SQL Server Management Studio (SSMS)

SQL Server Management Studio (SSMS) 是一个集成环境&#xff0c;用于管理任何SQL基础设施&#xff0c;从SQL Server到Azure SQL数据库。SSMS提供了各种工具来配置、监控和管理SQL Server的实体和组件。以下是一篇详细的使用指南&#xff0c;涵盖了SSMS的主要功能和操作。 1.…

《简单的逻辑学》

BEING LOGICAL 区分客观事实和主观观念 多数人的认知都来源于主观世界&#xff08;事实在大脑中的映射&#xff09;&#xff0c;很少有人探寻客观事实&#xff0c;所以有许多人在倡导&#xff0c;看待问题要探寻本质 多数命题的存在&#xff0c;都是有前提条件的 【黑白分明…

远程桌面连接工具Microsoft Remote Desktop Beta for Mac

Microsoft Remote Desktop Beta for Mac 是一款功能强大的远程桌面连接工具&#xff0c;具有以下功能特点&#xff1a; 软件下载地址 跨平台连接&#xff1a; 允许 Mac 用户轻松连接到运行 Windows 操作系统的计算机&#xff0c;打破了操作系统的界限&#xff0c;无论这些 Wi…