js综合案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            border: 0 none;
            outline-style: none;
        }
        
        .box {
            width: 380px;
            height: 400px;
            border: 1px solid #ccc;
            margin-left: 100px;
            margin-top: 100px;
            float: left;
        }
        
        .public {
            margin-top: 20px;
            padding: 0 20px;
        }
        
        .public input {
            width: 100%;
            height: 40px;
            border: 1px solid #ccc;
            margin-top: 10px;
            padding-left: 20px;
            box-sizing: border-box;
        }
        
        .public input:focus {
            border-color: orange;
        }
        
        textarea {
            width: 100%;
            height: 80px;
            border: 1px solid #ccc;
            margin-top: 10px;
            resize: none;
            padding-left: 20px;
            padding-top: 20px;
            box-sizing: border-box;
        }
        
        textarea:focus {
            border-color: orange;
        }
        
        .btn {
            display: block;
            width: 340px;
            height: 40px;
            margin: 20px auto;
            font-size: 20px;
            border-radius: 5px;
            font-weight: 700;
            cursor: pointer;
        }
        
        .show_stu {
            float: right;
            margin-right: 100px;
            margin-top: 100px;
            width: 500px;
            height: 400px;
            border: 1px solid #ccc;
            overflow: auto;
        }
        
        .title {
            height: 30px;
            line-height: 30px;
            border-bottom: 1px solid #ccc;
            width: 100%;
        }
        
        .title ul {
            /* width: 100%; */
            display: flex;
        }
        
        .title li {
            flex: 1;
            text-align: center;
            list-style: none;
        }
        
        .content li {
            margin-top: 10px;
            width: 100%;
            height: 40px;
            line-height: 40px;
            border-bottom: 1px dashed #ccc;
            text-overflow: ellipsis;
            overflow: hidden;
            display: flex;
        }
        
        .content li span {
            flex: 1;
            text-align: center;
        }
        
        .content li a:last-child {
            flex: 1;
            color: blue;
            cursor: pointer;
            text-align: center;
            text-decoration: none;
        }
    </style>
</head>

<body>

    <div class="box">
        <div class="stu_name public">
            <span>学生姓名:</span>
            <input type="text">
        </div>
        <div class="stu_score public">
            <span>考试成绩:</span>
            <input type="text">
        </div>
        <div class="msg public">
            <span>评语:</span>
            <textarea maxlength="20"></textarea>
        </div>
        <input type="button" value="添加" class="btn">
    </div>

    <div class="show_stu">
        <div class="title">
            <ul>
                <li>学生姓名</li>
                <li>考试成绩</li>
                <li>评语</li>
                <li>操作</li>
            </ul>
        </div>

        <div class="content">
            <ul>

            </ul>
        </div>
    </div>
   
</body>

</html>

先用HTML和CSS实现主要框架

 主要需求:填写学生信息,添加到页面

 主要思路:由于每个学生的信息为多条所以把每个一个学生看成一个具体对象,然后保存到数组中,在有数组中调用信息,实现数据的保存与传递

主要事件:点击事件添加,并在数据中可以操作数据进行删除

代码实现

<script>

       //使用document.querySelector方法获取各个标签,

        let btn = document.querySelector('.btn');
        let uls = document.querySelector('.content ul');
        let name = document.querySelector('.stu_name input');
        let score = document.querySelector('.stu_score input');
        let msg = document.querySelector('.msg textarea');
        //定义数组存储学生数据
        let arr = [];
        //绑定单机事件
        btn.onclick = function() {
        //  调试代码,测试是否能是否获得表单元素输入的值      
            /*console.log(name.value);
              console.log(score.value);
              console.log(msg.value);*/

        //使用字面量定义的方法定义学生对象

            let stu = {
        //  将表单元素的值赋值给对象的属性
                name: name.value,
                score: score.value,
                msg: msg.value

            }

        //利用数组的push方法对数据进行保存
            arr.push(stu);
        //  调试代码,测试是否能是否能获得对象的值  
            console.log(arr);
            console.log(arr[0].name);
        //利用document.createElement创建li标签
            let li = document.createElement('li');
        //使用insertBefore每次插入到ul的第一个元素之前
            uls.insertBefore(li, uls.children[0]);
        //因为保存到数组了,所以使用循环访问所有的值
            for (let i = 0; i < arr.length; i++) {
        //innerHTML,能够识别标签 用``插入自己的要添加的标签和使用${arr[i].score},进行值的插入
                li.innerHTML = `  <span> ${arr[i].name} </span>
                    <span>${arr[i].score}</span>
                        <span>${arr[i].msg}</span>
                            <span><a>删除</a></span>`
            }

        //当添加完毕后,清空表单输入的值
            name.value = '';
            score.value = '';
            msg.value = '';
         //获取最新添加的a标签

            let a = document.querySelector('.content a');
          //  由于a标签的是添加上去的且要添加事件,所以使用委托思想,获取a父级的父级
            d = a.parentNode.parentNode.parentNode;
            
            d.onclick = function(ev) {
                    


          //对点击元素进行判断,如果点击a标签才进行删除,此外都不会删除
          //使用事件对象参数进行判断,是否点击到删除才删除
                    if (ev.target.nodeName == 'A') {
          //对删除操作的进行提示
                        if (confirm('确定删除吗')) {
          //移除元素,this指向事件源
                            this.removeChild(a.parentNode.parentNode)
                        }

                     

                    }

                }
               


        }
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值