首页 > 编程语言 >【十一】JavaScript之案例-todolist

【十一】JavaScript之案例-todolist

时间:2023-07-04 14:35:23浏览次数:58  
标签:tasklist width todolist JavaScript height 案例 task taskData margin

【十一】JavaScript之案例-todolist

基本页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    body, ul, input{
        margin: 0;
        padding: 0;
    }
    ul{
        list-style: none;
    }
    .todolist{
        width: 600px;
        margin: 40px auto 0;
        background: #fff;
        box-shadow: 2px 2px 20px 0 rgba(0,0,0,.3);
    }
    .top{
        padding-top: 15px;
        margin: auto;
        width: 300px;
    }
    .top input[name="task"]{
        height: 22px;
        line-height: 22px;
        width: 204px;
        margin-right:1px;
        padding: 5px;
        font-size: 14px;
        border: 1px solid #aaa;
        outline: none;
        transition: all .5s linear;
    }
    .top input[name="task"]:hover{
        border: 1px solid #333;
    }
    .todolist button{
        height: 34px;
        line-height: 34px;
        font-size: 14px;
        border: none;
        outline: none;
        width: 80px;
        color: #333;
        background-color: #eee;
        cursor: pointer;
        transition: all .5s linear;
    }
    .todolist button:hover{
        background-color: #ccc;
    }
    .tasklist{
        width: 400px;
        margin: 20px auto;
        padding-bottom: 20px;
    }
    .tasklist li{
        font-size: 14px;
        height: 28px;
        line-height: 28px;
        margin-bottom: 4px;
        border-bottom: 1px solid #eee;
        color: #333;
        cursor: pointer;
        transition: all .5s linear;
    }
    .tasklist li:hover{
        border-bottom: 1px solid #aaa;
        background-color: #eee;
    }
    .tasklist li:after{
        display: block;
        content: "";
        clear: both;
    }
    .tasklist li .title{
        float: left;
    }
    .tasklist li button{
        float: right;
        height: 24px;
        line-height: 24px;
        width: 40px;
        margin: 0 5px;
        font-size: 12px;
    }
    </style>
</head>
<body>
    <div class="todolist">
        <div class="top">
            <input type="text" name="task" placeholder="请输入任务"><button class="btn">添加</button>
        </div>
        <ul class="tasklist">
            <li><span class="title">学习python</span><button>删除</button><button>↑</button><button>↓</button></li>
            <li><span class="title">学习python</span><button>删除</button><button>↑</button><button>↓</button></li>
            <li><span class="title">学习python</span><button>删除</button><button>↑</button><button>↓</button></li>
            <li><span class="title">学习python</span><button>删除</button><button>↑</button><button>↓</button></li>
        </ul>
    </div>
    <script>

    </script>
</body>
</html>

效果实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    body, ul, input{
        margin: 0;
        padding: 0;
    }
    ul{
        list-style: none;
    }
    .todolist{
        width: 600px;
        margin: 40px auto 0;
        background: #fff;
        box-shadow: 2px 2px 20px 0 rgba(0,0,0,.3);
    }
    .top{
        padding-top: 15px;
        margin: auto;
        width: 300px;
    }
    .top input[name="task"]{
        height: 22px;
        line-height: 22px;
        width: 204px;
        margin-right:1px;
        padding: 5px;
        font-size: 14px;
        border: 1px solid #aaa;
        outline: none;
        transition: all .5s linear;
    }
    .top input[name="task"]:hover{
        border: 1px solid #333;
    }
    .todolist button{
        height: 34px;
        line-height: 34px;
        font-size: 14px;
        border: none;
        outline: none;
        width: 80px;
        color: #333;
        background-color: #eee;
        cursor: pointer;
        transition: all .5s linear;
    }
    .todolist button:hover{
        background-color: #ccc;
    }
    .tasklist{
        width: 400px;
        margin: 20px auto;
        padding-bottom: 20px;
    }
    .tasklist li{
        font-size: 14px;
        height: 28px;
        line-height: 28px;
        margin-bottom: 4px;
        border-bottom: 1px solid #eee;
        color: #333;
        cursor: pointer;
        transition: all .5s linear;
    }
    .tasklist li:hover{
        border-bottom: 1px solid #aaa;
        background-color: #eee;
    }
    .tasklist li:after{
        display: block;
        content: "";
        clear: both;
    }
    .tasklist li .title{
        float: left;
    }
    .tasklist li button{
        float: right;
        height: 24px;
        line-height: 24px;
        width: 40px;
        margin: 0 5px;
        font-size: 12px;
    }
    </style>
</head>
<body>
    <div class="todolist">
        <div class="top">
            <input type="text" name="task" placeholder="请输入任务"><button class="btn">添加</button>
        </div>
        <ul class="tasklist">

        </ul>
    </div>
    <script>
    var task = document.querySelector('input[name="task"]');
    var btn = document.querySelector('.btn');
    var taskList = document.querySelector('.tasklist');
    var taskData = [
        "学习python",
        "学习java",
        "学习javascript",
        "学习Golang",
        "学习Rust",
    ]

    function showData(taskList, taskData){
        // 让数组中所有成员经过循环添加到ul列表中
        let content = ""
        for(let item of taskData.entries()){
            content += `
            <li>
                <span class="title">${item[1]}</span>
                <button onclick="deleteTask(${item[0]})">删除</button>
                <button onclick="moveUp(${item[0]})">↑</button>
                <button onclick="moveDown(${item[0]})">↓</button>
            </li>`
        }
        taskList.innerHTML = content;
    }

    showData(taskList, taskData);

    btn.onclick = function(){
        // 把用户添加的任务追到成员到taskData数组中
        taskData.push(task.value);
        // 显示数据
        showData(taskList, taskData);
        // 清空掉输入框的内容
        task.value = "";
    }

    function deleteTask(index){
        // 删除计划
        console.log("要删除的数据的下标:", index);
        // 根据下标删除数据中对应的计划任务
        taskData.splice(index,1);
        // 显示数据
        showData(taskList, taskData);
    }

    function moveUp(index){
        /**
         * 把计划向上移动
         */
        // 如果任务就在最上面,则不需要移动
        if(index === 0){
            return false; // 阻止代码继续往下执行
        }
        // 先把当前任务从任务列表中删除
        let task = taskData.splice(index,1)[0];
        console.log(task);
        // 在任务列表中的下标前一个位置插入
        taskData.splice(index-1, 0, task);
        // 显示数据
        showData(taskList, taskData);
    }


    function moveDown(index){
        /**
         * 把计划向下移动
         */
        // 如果任务就在最上面,则不需要移动
        if(index === taskData.length-1){
            return false; // 阻止代码继续往下执行
        }
        // 先把当前任务从任务列表中删除
        let task = taskData.splice(index,1)[0];
        // 在任务列表中的下标前一个位置插入
        taskData.splice(index+1, 0, task);
        // 显示数据
        showData(taskList, taskData);
    }
    </script>
</body>
</html>

标签:tasklist,width,todolist,JavaScript,height,案例,task,taskData,margin
From: https://www.cnblogs.com/dream-ze/p/17525635.html

相关文章

  • 【十四】JavaScript之DOM中的事件操作
    【十四】JavaScript之DOM中的事件操作【1】事件操作所谓的事件(Event),其实就是用户与浏览器之间的一次交互过程或交互行为。例如:用户输入一段内容,用户鼠标晃动一下等等。js中提供了大量的事件操作给开发者,在用户发生事件操作以后,进行反馈,响应。事件类型有很多,但是操......
  • 【十三】JavaScript之DOM
    【十三】JavaScript之DOM【1】DOMDOM(DocumentObjectModel,译作文档对象模型),这部分内容主要是学习document对象提供给开发者操作html/xml文档的方法属性或子对象来完成动态特效的。当然这部分代码在不同浏览器下的效果几乎一样,除了IE。元素操作[元素的获取,元素的属性......
  • EtherCAT 转CCLINK网关连接三菱plc应用案例
    EtherCAT现场总线协议是由德国倍福公司在2003年提出的,该通讯协议拓扑结构十分灵活,数据传输速度快,同步特性好,可以形成各种网络拓扑结构。捷米特JM-ECT-CCLK 是自主研发的一款CCLINK从站功能的通讯网关。该产品主要功能是将各种CCLINK总线和ETHERCAT网络连接起来。本网......
  • web开发基础笔录(5)-Javascript(1)
    目录概述概述JavaScript(简称“JS”)是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。虽然它是作为开发Web页面的脚本语言而出名,但是它也被用到了很多非浏览器环境中,JavaScript基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式、声明式、函数式编程范式。......
  • 初学者:8个JavaScript技巧
    有很多方法可以优化我们的JavaScript代码,本文总结了我在工作中经常使用的8个JavaScript技巧,希望它也能帮助你。减少使用if-else在编写两个以上的if...else时,是否有更好的优化方法?如下代码,我们需要根据一个汉堡包的名字来计算它的价格。constgetPriceByName=(na......
  • JavaScript中的if与switch的区别是什么?
    很多同学问我if和swicth的区别,今天我就从多个维度来和大家分析一下if与switch的区别到底是什么?1.语法结构:if语句:使用if关键字后接条件表达式,如果条件为真,则执行if块中的代码。switch语句:使用switch关键字后接一个表达式,根据表达式的值匹配相应的case标签,并执行对应的代码块。2.可读......
  • JavaScript中的if与switch的区别是什么?
    很多同学问我if和swicth的区别,今天我就从多个维度来和大家分析一下if与switch的区别到底是什么?1.语法结构:if语句:使用if关键字后接条件表达式,如果条件为真,则执行if块中的代码。switch语句:使用switch关键字后接一个表达式,根据表达式的值匹配相应的case标签,并执行对应的代码块。2.可读......
  • JavaScript(四)面向对象
    创建对象prototype对象JavaScript对每个创建的对象都会设置一个原型,指向它的原型对象。当我们用obj.xxx访问一个对象的属性时,JavaScript引擎先在当前对象上查找该属性,如果没有找到,就到其原型对象上找,如果还没有找到,就一直上溯到Object.prototype对象,最后,如果还没有找到,就只能返......
  • JavaScript(三)Array的高阶函数
    map、reducemap:map()方法定义在JavaScript的Array中,接收一个函数对象作为参数,函数定义运算规则,对array中的每个元素进行运算,结果是一个新的array。functionpow(x){returnx*x;}vararr=[1,2,3,4,5,6,7,8,9];varresults=arr.map(pow);//[1,4,9......
  • JavaScript(一)基础
    JS引入到文件嵌入到html文件中,在<header>或<body>中使用<script><script> vari=10; console.log(i);</script>引入JS文件,在<header>或<body>中使用<script><scriptsrc="./index3_script.js"type="text/j......