JS(JavaScript)事件处理(事件绑定)趣味案例

news2024/10/7 10:23:52

天行健,君子以自强不息;地势坤,君子以厚德载物。


每个人都有惰性,但不断学习是好好生活的根本,共勉!


文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。


谁家玉笛暗飞声,散入春风满洛城。
此夜曲中闻折柳,何人不起故园情。
——《春夜洛城闻笛》


文章目录

  • JS(JavaScript)事件处理(事件绑定)趣味案例
    • 1. 创建表格
      • 1.1 示例代码
      • 1.2 页面效果
    • 2. 创建表单
      • 2.1 示例代码
      • 2.2 页面效果
    • 3. 添加样式
      • 3.1 示例代码
      • 3.2 页面效果
    • 4. 表格中的事件绑定
      • 4.1 示例代码
      • 4.2 页面效果
    • 5. 表单中的事件绑定
      • 5.1 示例代码
      • 5.2 页面效果
    • 6. 复选框功能实现
      • 6.1 示例代码
      • 6.2 页面效果
    • 7. 示例代码下载


JS(JavaScript)入门指南
JS(JavaScript)入门指南(DOM、事件处理、BOM、数据校验)
JS(JavaScript)学习专栏


JS(JavaScript)事件处理(事件绑定)趣味案例

通过创建一个页面来实现事件处理相关的操作,以下为实例演示部分,主要针对dom操作表格以及事件相关的操作。

1. 创建表格

创建一个表格,表格每行最后有一个按钮,按钮为删除当前行的事件
表格页脚有两个按钮:首行删除和末行删除

1.1 示例代码

示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用dom操作表格-创建一个表格</title>

</head>
<body>
    
    <!-- 将表格放到div块中,方便后续操作 -->
    <div id="container">
        <!-- 表格 -->
        <table id="tt">

            <!-- 表头 -->
            <thead>
                <tr>
                    <th>召唤师</th>
                    <th>英雄</th>
                    <th>定位</th>
                    <th>金币</th>
                    <th>技能</th>
                </tr>
            </thead>

            <!-- 表格体 -->
            <tbody id="tb">
                <tr>
                    <th>寒山</th>
                    <th>李白</th>
                    <th>刺客</th>
                    <th>38000</th>
                    <th><input type="button" value="惊鸿一剑" class="del"></th>
                </tr>
                <tr>
                    <th>李四</th>
                    <th>廉颇</th>
                    <th>坦克</th>
                    <th>11800</th>
                    <th><input type="button" value="地动山摇" class="del"></th>
                </tr>
                <tr>
                    <th>赵五</th>
                    <th>貂蝉</th>
                    <th>法师</th>
                    <th>38000</th>
                    <th><input type="button" value="随风起舞" class="del"></th>
                </tr>
                <tr>
                    <th>王二</th>
                    <th>后羿</th>
                    <th>射手</th>
                    <th>8800</th>
                    <th><input type="button" value="百步穿杨" class="del"></th>
                </tr>
            </tbody>

            <!-- 表格页脚 -->
            <tfoot>
                <tr>
                    <td colspan="5">
                        <input type="button" value="从首行删除" id="btnDelFirst">
                        <input type="button" value="从末行删除" id="btnDelLast">
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</body>
</html>

1.2 页面效果

使用浏览器打开页面,效果如下,此时按钮并未生效,需要后续绑定事件
在这里插入图片描述

2. 创建表单

在前面表格的基础上,创建一个表单,表单中设置的是input输入,有文本、单选、按钮,用来对上面的表格进行数据的操作

2.1 示例代码

表单示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用dom操作表格-创建一个form表单</title>

</head>
<body>
    
    <div id="container">
        <!-- 表格 -->
        <table id="tt">

            <!-- 表头 -->
            <thead>
                <tr>
                    <th>召唤师</th>
                    <th>英雄</th>
                    <th>定位</th>
                    <th>金币</th>
                    <th>技能</th>
                </tr>
            </thead>

            <!-- 表格体 -->
            <tbody id="tb">
                <tr>
                    <th>寒山</th>
                    <th>李白</th>
                    <th>刺客</th>
                    <th>38000</th>
                    <th><input type="button" value="惊鸿一剑" class="del"></th>
                </tr>
                <tr>
                    <th>李四</th>
                    <th>廉颇</th>
                    <th>坦克</th>
                    <th>11800</th>
                    <th><input type="button" value="地动山摇" class="del"></th>
                </tr>
                <tr>
                    <th>赵五</th>
                    <th>貂蝉</th>
                    <th>法师</th>
                    <th>38000</th>
                    <th><input type="button" value="随风起舞" class="del"></th>
                </tr>
                <tr>
                    <th>王二</th>
                    <th>后羿</th>
                    <th>射手</th>
                    <th>8800</th>
                    <th><input type="button" value="百步穿杨" class="del"></th>
                </tr>
            </tbody>

            <!-- 表格页脚 -->
            <tfoot>
                <tr>
                    <td colspan="5">
                        <input type="button" value="从首行删除" id="btnDelFirst">
                        <input type="button" value="从末行删除" id="btnDelLast">
                    </td>
                </tr>
            </tfoot>
        </table>

        <hr>
    
        <!-- form表单创建 -->
        <form action="" id="formId">
            召唤师:<input type="text" id="name"><br>
            英雄:<input type="text" id="heroName"><br>
            定位:<input type="radio" name="actor" value="tank" id="tank" checked> 坦克 
                  <input type="radio" name="actor" value="warrior" id="warrior"> 战士 
                  <input type="radio" name="actor" value="assassin" id="assassin"> 刺客
                  <input type="radio" name="actor" value="master" id="master"> 法师
                  <input type="radio" name="actor" value="shooter" id="shooter"> 射手
                  <input type="radio" name="actor" value="addjunct" id="addjunct"> 辅助<br> 
            价格:<input type="text" id="price"><br>
            技能:<input type="text" id="func"><br>
                  <input type="button" value="添  加" id="btnAdd">
                  <input type="button" value="重  置">
        </form>
    </div>
    
</body>
</html>

2.2 页面效果

使用浏览器打开页面,如下,表单中的事件也并未绑定,暂时无效
在这里插入图片描述

3. 添加样式

为表格和表单添加样式,优化以下视觉效果

3.1 示例代码

示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用dom操作表格-为表格和表单添加样式</title>

    <style>
        /* 设置整个div块在页面中的位置 */
        #container{
            /* 居中 */
            text-align: center;
        }

        /* 设置table表格的样式 */
        #tt{
            /* 宽度 */
            width: 500px;
            /* 居中 */
            text-align: center;
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
            /* 边框设置 上下设为0 左右设为自动 */
            margin: 0 auto;
        }

        /* table下的td和th样式设置 */
        #tt td, #tt th{
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
        }

        /* table下的页脚的td样式设置 */
        #tt tfoot td{
            /* 靠右 */
            text-align: right;
        }

        /* 表单的样式设置 */
        #formId{
            /* 每行的间隔设置 */
            line-height: 30px;
        }
    </style>

</head>
<body>
    
    <div id="container">
        <!-- 表格 -->
        <table id="tt">

            <!-- 表头 -->
            <thead>
                <tr>
                    <th>召唤师</th>
                    <th>英雄</th>
                    <th>定位</th>
                    <th>金币</th>
                    <th>技能</th>
                </tr>
            </thead>

            <!-- 表格体 -->
            <tbody id="tb">
                <tr>
                    <th>寒山</th>
                    <th>李白</th>
                    <th>刺客</th>
                    <th>38000</th>
                    <th><input type="button" value="惊鸿一剑" class="del"></th>
                </tr>
                <tr>
                    <th>李四</th>
                    <th>廉颇</th>
                    <th>坦克</th>
                    <th>11800</th>
                    <th><input type="button" value="地动山摇" class="del"></th>
                </tr>
                <tr>
                    <th>赵五</th>
                    <th>貂蝉</th>
                    <th>法师</th>
                    <th>38000</th>
                    <th><input type="button" value="随风起舞" class="del"></th>
                </tr>
                <tr>
                    <th>王二</th>
                    <th>后羿</th>
                    <th>射手</th>
                    <th>8800</th>
                    <th><input type="button" value="百步穿杨" class="del"></th>
                </tr>
            </tbody>

            <!-- 表格页脚 -->
            <tfoot>
                <tr>
                    <td colspan="5">
                        <input type="button" value="从首行删除" id="btnDelFirst">
                        <input type="button" value="从末行删除" id="btnDelLast">
                    </td>
                </tr>
            </tfoot>
        </table>

        <hr>
    
        <!-- form表单创建 -->
        <form action="" id="formId">
            召唤师:<input type="text" id="name"><br>
            英雄:<input type="text" id="heroName"><br>
            定位:<input type="radio" name="actor" value="tank" id="tank" checked> 坦克 
                  <input type="radio" name="actor" value="warrior" id="warrior"> 战士 
                  <input type="radio" name="actor" value="assassin" id="assassin"> 刺客
                  <input type="radio" name="actor" value="master" id="master"> 法师
                  <input type="radio" name="actor" value="shooter" id="shooter"> 射手
                  <input type="radio" name="actor" value="addjunct" id="addjunct"> 辅助<br> 
            价格:<input type="text" id="price"><br>
            技能:<input type="text" id="func"><br>
                  <input type="button" value="添  加" id="btnAdd">
                  <input type="button" value="重  置">
        </form>
    </div>
    
</body>
</html>

3.2 页面效果

使用浏览器打开页面,如下,看起来好一点点
在这里插入图片描述

4. 表格中的事件绑定

先为表格中的按钮绑定事件,编写js代码

4.1 示例代码

示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用dom操作表格-为表格中的按钮绑定事件</title>

    <style>
        /* 设置整个div块在页面中的位置 */
        #container{
            /* 居中 */
            text-align: center;
        }

        /* 设置table表格的样式 */
        #tt{
            /* 宽度 */
            width: 500px;
            /* 居中 */
            text-align: center;
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
            /* 边框设置 上下设为0 左右设为自动 */
            margin: 0 auto;
        }

        /* table下的td和th样式设置 */
        #tt td, #tt th{
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
        }

        /* table下的页脚的td样式设置 */
        #tt tfoot td{
            /* 靠右 */
            text-align: right;
        }

        /* 表单的样式设置 */
        #formId{
            /* 每行的间隔设置 */
            line-height: 30px;
        }
    </style>

    <script>

        //页面加载完之后触发事件
        window.onload=function(){
            //为技能按钮绑定事件(删除,点击技能释放大招,删除当前行)
            var btFuns = document.querySelectorAll(".del");
            //遍历所有按钮,移出当前行,即tr
            for(var i=0;i<btFuns.length;i++){
                btFuns[i].onclick=function(){
                    //当前获取的位置是input标签,其父标签的父标签为tr标签
                    this.parentNode.parentNode.remove();
                }
            }

            //为首行删除按钮绑定事件
            $("btnDelFirst").onclick=function(){
                //通过获取tb的document对象,然后通过标签tr获取集合,删除第一个tr
                $("tb").getElementsByTagName("tr")[0].remove();
            }
            //为末行删除按钮绑定事件
            $("btnDelLast").onclick=function(){
                //获取tb下的tr集合
                var trs = $("tb").getElementsByTagName("tr");
                //移出最后一个tr
                trs[trs.length-1].remove();;
            }
        }

        //获取元素的函数
        function $(id){
            return document.getElementById(id);
        }

    </script>

</head>
<body>
    
    <div id="container">
        <!-- 表格 -->
        <table id="tt">

            <!-- 表头 -->
            <thead>
                <tr>
                    <th>召唤师</th>
                    <th>英雄</th>
                    <th>定位</th>
                    <th>金币</th>
                    <th>技能</th>
                </tr>
            </thead>

            <!-- 表格体 -->
            <tbody id="tb">
                <tr>
                    <th>寒山</th>
                    <th>李白</th>
                    <th>刺客</th>
                    <th>38000</th>
                    <th><input type="button" value="惊鸿一剑" class="del"></th>
                </tr>
                <tr>
                    <th>李四</th>
                    <th>廉颇</th>
                    <th>坦克</th>
                    <th>11800</th>
                    <th><input type="button" value="地动山摇" class="del"></th>
                </tr>
                <tr>
                    <th>赵五</th>
                    <th>貂蝉</th>
                    <th>法师</th>
                    <th>38000</th>
                    <th><input type="button" value="随风起舞" class="del"></th>
                </tr>
                <tr>
                    <th>王二</th>
                    <th>后羿</th>
                    <th>射手</th>
                    <th>8800</th>
                    <th><input type="button" value="百步穿杨" class="del"></th>
                </tr>
            </tbody>

            <!-- 表格页脚 -->
            <tfoot>
                <tr>
                    <td colspan="5">
                        <input type="button" value="从首行删除" id="btnDelFirst">
                        <input type="button" value="从末行删除" id="btnDelLast">
                    </td>
                </tr>
            </tfoot>
        </table>

        <hr>
    
        <!-- form表单创建 -->
        <form action="" id="formId">
            召唤师:<input type="text" id="name"><br>
            英雄:<input type="text" id="heroName"><br>
            定位:<input type="radio" name="actor" value="tank" id="tank" checked> 坦克 
                  <input type="radio" name="actor" value="warrior" id="warrior"> 战士 
                  <input type="radio" name="actor" value="assassin" id="assassin"> 刺客
                  <input type="radio" name="actor" value="master" id="master"> 法师
                  <input type="radio" name="actor" value="shooter" id="shooter"> 射手
                  <input type="radio" name="actor" value="addjunct" id="addjunct"> 辅助<br> 
            价格:<input type="text" id="price"><br>
            技能:<input type="text" id="func"><br>
                  <input type="button" value="添  加" id="btnAdd">
                  <input type="button" value="重  置">
        </form>
    </div>
    
</body>
</html>

4.2 页面效果

使用浏览器打开页面,如下,此时可以点击技能框里的技能进行删除当前行,也可以点击右下角的首行删除和末行删除进行首行和末行的删除
在这里插入图片描述
在这里插入图片描述

5. 表单中的事件绑定

5.1 示例代码

示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用dom操作表格-为表单中的按钮绑定事件</title>

    <style>
        /* 设置整个div块在页面中的位置 */
        #container{
            /* 居中 */
            text-align: center;
            /* background-color: lightblue; */
        }

        /* 设置table表格的样式 */
        #tt{
            /* 宽度 */
            width: 500px;
            /* 居中 */
            text-align: center;
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
            /* 边框设置 上下设为0 左右设为自动 */
            margin: 0 auto;
            /* 表格背景色 */
            /* background-color: lightblue; */
        }

        /* table下的td和th样式设置 */
        #tt td, #tt th{
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
        }

        /* table下的页脚的td样式设置 */
        #tt tfoot td{
            /* 靠右 */
            text-align: right;
        }

        /* 表单的样式设置 */
        #formId{
            /* 每行的间隔设置 */
            line-height: 30px;
        }
    </style>

    <script>

        //页面加载完之后触发事件
        window.onload=function(){
            //为添加按钮绑定事件
            // document.getElementById("btnAdd")
            $("btnAdd").onclick=function(){
                //创建tr
                var tr = document.createElement("tr");
                //创建td
                var tdName = document.createElement("td");
                var tdHeroName = document.createElement("td");
                var tdActor = document.createElement("td");
                var tdPrice = document.createElement("td");
                var tdFun = document.createElement("td");
                //将数据添加到td中
                tdName.innerHTML=$("name").value;
                tdHeroName.innerHTML=$("heroName").value;
                
                // tdActor.innerHTML=$("actor").value;
                //根据勾选的位置将对应的值填入定位一栏
                if($("tank").checked){
                    tdActor.innerHTML=$("tank").value;
                }
                if($("warrior").checked){
                    tdActor.innerHTML=$("warrior").value;
                }
                if($("assassin").checked){
                    tdActor.innerHTML=$("assassin").value;
                }
                if($("master").checked){
                    tdActor.innerHTML=$("master").value;
                }
                if($("shooter").checked){
                    tdActor.innerHTML=$("shooter").value;
                }
                if($("addjunct").checked){
                    tdActor.innerHTML=$("addjunct").value;
                }
                tdPrice.innerHTML=$("price").value;
                // tdFun.innerHTML=$("func").value;
                //创建按钮
                var btnFun = document.createElement("input");
                btnFun.type = "button";
                btnFun.value = $("func").value;
                //为按钮绑定事件用于删除
                btnFun.onclick = function(){
                    this.parentNode.parentNode.remove();
                }
                tdFun.appendChild(btnFun);

                //将td添加到tr中
                tr.appendChild(tdName);
                tr.appendChild(tdHeroName);
                tr.appendChild(tdActor);
                tr.appendChild(tdPrice);
                tr.appendChild(tdFun);

                //将tr添加到tbody中
                $("tb").appendChild(tr);
            }

            //为技能按钮绑定事件(删除,点击技能释放大招,删除当前行)
            var btFuns = document.querySelectorAll(".del");
            //遍历所有按钮,移出当前行,即tr
            for(var i=0;i<btFuns.length;i++){
                btFuns[i].onclick=function(){
                    //当前获取的位置是input标签,其父标签的父标签为tr标签
                    this.parentNode.parentNode.remove();
                }
            }

            //为首行删除按钮绑定事件
            $("btnDelFirst").onclick=function(){
                //通过获取tb的document对象,然后通过标签tr获取集合,删除第一个tr
                $("tb").getElementsByTagName("tr")[0].remove();
            }
            //为末行删除按钮绑定事件
            $("btnDelLast").onclick=function(){
                //获取tb下的tr集合
                var trs = $("tb").getElementsByTagName("tr");
                //移出最后一个tr
                trs[trs.length-1].remove();;
            }
        }

        //获取元素的函数
        function $(id){
            return document.getElementById(id);
        }

    </script>

</head>
<body>
    
    <div id="container">
        <!-- 表格 -->
        <table id="tt">

            <!-- 表头 -->
            <thead>
                <tr>
                    <th>召唤师</th>
                    <th>英雄</th>
                    <th>定位</th>
                    <th>金币</th>
                    <th>技能</th>
                </tr>
            </thead>

            <!-- 表格体 -->
            <tbody id="tb">
                <tr>
                    <th>寒山</th>
                    <th>李白</th>
                    <th>刺客</th>
                    <th>38000</th>
                    <th><input type="button" value="惊鸿一剑" class="del"></th>
                </tr>
                <tr>
                    <th>李四</th>
                    <th>廉颇</th>
                    <th>坦克</th>
                    <th>11800</th>
                    <th><input type="button" value="地动山摇" class="del"></th>
                </tr>
                <tr>
                    <th>赵五</th>
                    <th>貂蝉</th>
                    <th>法师</th>
                    <th>38000</th>
                    <th><input type="button" value="随风起舞" class="del"></th>
                </tr>
                <tr>
                    <th>王二</th>
                    <th>后羿</th>
                    <th>射手</th>
                    <th>8800</th>
                    <th><input type="button" value="百步穿杨" class="del"></th>
                </tr>
            </tbody>

            <!-- 表格页脚 -->
            <tfoot>
                <tr>
                    <td colspan="5">
                        <input type="button" value="从首行删除" id="btnDelFirst">
                        <input type="button" value="从末行删除" id="btnDelLast">
                    </td>
                </tr>
            </tfoot>
        </table>

        <hr>
    
        <!-- form表单创建 -->
        <form action="" id="formId">
            召唤师:<input type="text" id="name"><br>
            英雄:<input type="text" id="heroName"><br>
            定位:<input type="radio" name="actor" value="tank" id="tank" checked> 坦克 
                  <input type="radio" name="actor" value="warrior" id="warrior"> 战士 
                  <input type="radio" name="actor" value="assassin" id="assassin"> 刺客
                  <input type="radio" name="actor" value="master" id="master"> 法师
                  <input type="radio" name="actor" value="shooter" id="shooter"> 射手
                  <input type="radio" name="actor" value="addjunct" id="addjunct"> 辅助<br> 
            价格:<input type="text" id="price"><br>
            技能:<input type="text" id="func"><br>
                  <input type="button" value="添  加" id="btnAdd">
                  <input type="button" value="重  置">
        </form>
    </div>
    
</body>
</html>

5.2 页面效果

使用浏览器打开页面,如下,此时可以填写召唤师、英雄、价格、技能,然后勾选定位的位置,进行添加信息到表格中
在这里插入图片描述
在这里插入图片描述

6. 复选框功能实现

在每行最前面加上复选框按钮,最上面的按钮是全选按钮。
全选按钮选中后,下方所有复选框都自动勾选,反之亦然。
下方复选框全部勾选后,全选按钮的复选框自动勾选。
对新增的行也要满足。
在复选框那列最先放设置删除所勾选的内容按钮,可删除所有勾选的内容。

6.1 示例代码

示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用dom操作表格-添加复选框并绑定事件</title>

    <style>
        /* 设置整个div块在页面中的位置 */
        #container{
            /* 居中 */
            text-align: center;
            /* background-color: lightblue; */
        }

        /* 设置table表格的样式 */
        #tt{
            /* 宽度 */
            width: 500px;
            /* 居中 */
            text-align: center;
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
            /* 边框设置 上下设为0 左右设为自动 */
            margin: 0 auto;
            /* 表格背景色 */
            /* background-color: lightblue; */
        }

        /* table下的td和th样式设置 */
        #tt td, #tt th{
            /* 边界设置 实线 灰色 */
            border: 1px solid #ccc;
        }

        /* table下的页脚的td样式设置 */
        #tt tfoot td{
            /* 靠右 */
            text-align: right;
        }

        /* 表单的样式设置 */
        #formId{
            /* 每行的间隔设置 */
            line-height: 30px;
        }

        /* 复选框删除按钮样式设置 */
        #tt tfoot tr td{
            text-align: center;
        }
    </style>

    <script>

        //页面加载完之后触发事件
        window.onload=function(){
            //为添加按钮绑定事件
            // document.getElementById("btnAdd")
            $("btnAdd").onclick=function(){
                //创建tr
                var tr = document.createElement("tr");

                //创建td
                var tdName = document.createElement("td");
                var tdHeroName = document.createElement("td");
                var tdActor = document.createElement("td");
                var tdPrice = document.createElement("td");
                var tdFun = document.createElement("td");
                //添加复选框
                var tdCheckbox = document.createElement("td");

                //将数据添加到td中
                tdName.innerHTML=$("name").value;
                tdHeroName.innerHTML=$("heroName").value;
                
                // tdActor.innerHTML=$("actor").value;
                //根据勾选的位置将对应的值填入定位一栏
                if($("tank").checked){
                    tdActor.innerHTML=$("tank").value;
                }
                if($("warrior").checked){
                    tdActor.innerHTML=$("warrior").value;
                }
                if($("assassin").checked){
                    tdActor.innerHTML=$("assassin").value;
                }
                if($("master").checked){
                    tdActor.innerHTML=$("master").value;
                }
                if($("shooter").checked){
                    tdActor.innerHTML=$("shooter").value;
                }
                if($("addjunct").checked){
                    tdActor.innerHTML=$("addjunct").value;
                }

                tdPrice.innerHTML=$("price").value;
                // tdFun.innerHTML=$("func").value;
                //创建按钮
                var btnFun = document.createElement("input");
                btnFun.type = "button";
                btnFun.value = $("func").value;
                //为按钮绑定事件用于删除
                btnFun.onclick = function(){
                    this.parentNode.parentNode.remove();
                }
                tdFun.appendChild(btnFun);

                //为复选框添加属性
                var checkboxInfo = document.createElement("input");
                checkboxInfo.type="checkbox";
                checkboxInfo.className="checkboxed";
                checkboxInfo.onclick=function(){
                    var checks = document.querySelectorAll(".checkboxed");
                    //统计已选中的数量
                    var count = 0;
                    for(var j=0; j<checks.length; j++){
                        if(checks[j].checked){
                            count++;
                        }
                    }
                    //判断是否全部选中
                    if(count==checks.length){
                        $("all").checked=true;
                    }else{
                        $("all").checked=false;
                    }
                };
                //将复选框的input标签添加到td中
                tdCheckbox.appendChild(checkboxInfo);

                //将td添加到tr中
                tr.appendChild(tdCheckbox);
                tr.appendChild(tdName);
                tr.appendChild(tdHeroName);
                tr.appendChild(tdActor);
                tr.appendChild(tdPrice);
                tr.appendChild(tdFun);

                //将tr添加到tbody中
                $("tb").appendChild(tr);
            };

            //为技能按钮绑定事件(删除,点击技能释放大招,删除当前行)
            var btFuns = document.querySelectorAll(".del");
            //遍历所有按钮,移出当前行,即tr
            for(var i=0;i<btFuns.length;i++){
                btFuns[i].onclick=function(){
                    //当前获取的位置是input标签,其父标签的父标签为tr标签
                    this.parentNode.parentNode.remove();
                }
            }

            //为首行删除按钮绑定事件
            $("btnDelFirst").onclick=function(){
                //通过获取tb的document对象,然后通过标签tr获取集合,删除第一个tr
                $("tb").getElementsByTagName("tr")[0].remove();
            };
            //为末行删除按钮绑定事件
            $("btnDelLast").onclick=function(){
                //获取tb下的tr集合
                var trs = $("tb").getElementsByTagName("tr");
                //移出最后一个tr
                trs[trs.length-1].remove();;
            };

            // 为全选绑定事件
            $("all").onclick=function(){
                //如果全选框勾上,触发事件所有复选框全部选中
                if($("all").checked){
                    var checkeds = document.querySelectorAll(".checkboxed");
                    for(var i=0; i<checkeds.length; i++){
                        checkeds[i].checked=true;
                    }
                }else{
                    var checkeds = document.querySelectorAll(".checkboxed");
                    for(var i=0; i<checkeds.length; i++){
                        checkeds[i].checked=false;
                    }
                }
            };

            //实现选中所有复选框时,全选自动勾选
            var checks = document.querySelectorAll(".checkboxed");
            for(var i=0; i<checks.length; i++){
                checks[i].onclick=function(){
                    var checks = document.querySelectorAll(".checkboxed");
                    //统计已选中的数量
                    var count = 0;
                    for(var j=0; j<checks.length; j++){
                        if(checks[j].checked){
                            count++;
                        }
                    }
                    //判断是否全部选中
                    if(count==checks.length){
                        $("all").checked=true;
                    }else{
                        $("all").checked=false;
                    }
                };
            }

            //为删除所勾选内容的按钮绑定事件
            $("delChecked").onclick=function(){
                var checks = document.querySelectorAll(".checkboxed");
                for(var i=0; i<checks.length; i++){
                    if(checks[i].checked){
                        checks[i].parentNode.parentNode.remove();
                    }
                }
            }


        };

        //获取元素的函数
        function $(id){
            return document.getElementById(id);
        }

    </script>

</head>
<body>
    
    <div id="container">
        <!-- 表格 -->
        <table id="tt">

            <!-- 表头 -->
            <thead>
                <tr>
                    <th>
                        全选 <input type="checkbox" id="all">
                    </th>
                    <th>召唤师</th>
                    <th>英雄</th>
                    <th>定位</th>
                    <th>金币</th>
                    <th>技能</th>
                </tr>
            </thead>

            <!-- 表格体 -->
            <tbody id="tb">
                <tr>
                    <th><input type="checkbox" class="checkboxed"></th>
                    <th>寒山</th>
                    <th>李白</th>
                    <th>刺客</th>
                    <th>38000</th>
                    <th><input type="button" value="惊鸿一剑" class="del"></th>
                </tr>
                <tr>
                    <th><input type="checkbox" class="checkboxed"></th>
                    <th>李四</th>
                    <th>廉颇</th>
                    <th>坦克</th>
                    <th>11800</th>
                    <th><input type="button" value="地动山摇" class="del"></th>
                </tr>
                <tr>
                    <th><input type="checkbox" class="checkboxed"></th>
                    <th>赵五</th>
                    <th>貂蝉</th>
                    <th>法师</th>
                    <th>38000</th>
                    <th><input type="button" value="随风起舞" class="del"></th>
                </tr>
                <tr>
                    <th><input type="checkbox" class="checkboxed"></th>
                    <th>王二</th>
                    <th>后羿</th>
                    <th>射手</th>
                    <th>8800</th>
                    <th><input type="button" value="百步穿杨" class="del"></th>
                </tr>
            </tbody>

            <!-- 表格页脚 -->
            <tfoot>
                <tr>
                    <td>
                        <input type="button" value="删除所勾选内容" id="delChecked">
                    </td>
                    <td colspan="5">
                        <input type="button" value="从首行删除" id="btnDelFirst">
                        <input type="button" value="从末行删除" id="btnDelLast">
                    </td>
                </tr>
            </tfoot>
        </table>

        <hr>
    
        <!-- form表单创建 -->
        <form action="" id="formId">
            召唤师:<input type="text" id="name"><br>
            英雄:<input type="text" id="heroName"><br>
            定位:<input type="radio" name="actor" value="tank" id="tank" checked> 坦克 
                  <input type="radio" name="actor" value="warrior" id="warrior"> 战士 
                  <input type="radio" name="actor" value="assassin" id="assassin"> 刺客
                  <input type="radio" name="actor" value="master" id="master"> 法师
                  <input type="radio" name="actor" value="shooter" id="shooter"> 射手
                  <input type="radio" name="actor" value="addjunct" id="addjunct"> 辅助<br> 
            价格:<input type="text" id="price"><br>
            技能:<input type="text" id="func"><br>
                  <input type="button" value="添  加" id="btnAdd">
                  <input type="button" value="重  置">
        </form>
    </div>
    
</body>
</html>

6.2 页面效果

使用浏览器打开页面,如下
在这里插入图片描述

7. 示例代码下载

本文示例代码文件已上传至CSDN资源库
下载地址:JavaScript 趣味案例-事件处理-dom操作表格 示例代码


感谢阅读,祝君暴富!


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1874823.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

c语言--指针

前言 欢迎来到我的博客 个人主页:北岭敲键盘的荒漠猫-CSDN博客 本文整理c语言中指针的相关知识点。 指针概念 指针存储的就是数据的地址。 直观理解: 李华家是北洋路130号1单元101 用变量处理数据: 我们去李华家拿数据。 用指针处理数据: 我们去北洋路130号1单元101拿数据…

float8格式

产生背景 在人工智能神经元网络中&#xff0c;一个参数用1字节表示即可&#xff0c;或者说&#xff0c;这是个猜想&#xff1a;因为图像的颜色用8比特表示就够了&#xff0c;所以说&#xff0c;猜想神经元的区分度应该小于256。 数字的分配 8比特有256个码位&#xff0c;分为…

免费分享:2015-2020年中国区域温度植被干旱指数(TVDI)月数据(附下载方法)

温度植被干旱指数(Temperature Vegetation Dryness Index&#xff0c;TVDI)是一种基于归一化植被指数(NDVI)与地表温度(LST)的土壤水分反演模型&#xff0c;能较好的进行干旱监测&#xff0c;尤其是监测特定年内某一时期整个区域的相对干旱程度&#xff0c;并可用于研究干旱程度…

华为牵手居然之家,智能家居惊喜可期

在这个科技飞速发展的时代&#xff0c;智能家居已逐渐从概念走向现实。华为&#xff0c;作为全球知名的信息与通信技术解决方案提供商&#xff0c;近期与居然之家达成战略合作&#xff0c;入局智能家居领域&#xff0c;无疑为我们带来了无限的遐想和期待。 华为一直以来在技术创…

8个成功的原型设计案例分享

原型设计在整个产品设计过程中非常的重要&#xff0c;定下了整个产品的基调&#xff0c;想要做好原型设计&#xff0c;需要不断的经验的积累&#xff0c;新手入门最快的方式就是学习一些优秀的原型设计案例&#xff0c;今天就为大家分享一些可以直接使用源文件进行编辑和修改的…

【网络架构】keepalive

目录 一、keepalive基础 1.1 作用 1.2 原理 1.3 功能 二、keepalive安装 2.1 yum安装 2.2 编译安装 三、配置文件 3.1 keepalived相关文件 3.2 主配置的组成 3.2.1 全局配置 3.2.2 配置虚拟路由器 四、实际操作 4.1 lvskeepalived高可用群集 4.2 keepalivedngi…

【算法专题--栈】后缀表达式求值 -- 高频面试题(图文详解,小白一看就会!!)

目录 一、前言 二、题目描述 三、解题方法 ⭐解题思路 ⭐案例图解 四、总结与提炼 五、共勉 一、前言 后缀表达式求值 这道题&#xff0c;可以说是--栈专题--&#xff0c;最经典的一道题&#xff0c;也是在面试中频率最高的一道题目&#xff0c;通常在面试中&…

vue3封装表格嵌套表单问题汇总

1.插槽嵌套多层数据ui组件怎么使用 思路&#xff1a;插槽具名【区分】后暴露传递&#xff0c;这个为神魔要区分&#xff0c;因为封装组件表格列表项也有插槽 步骤一&#xff1a;表单插槽暴露 <ElFormclass"form-search":model"formParams"ref"form…

MS5208T/MS5208N——2.7V 到 5.5V、 12Bit、8 通道轨到轨输出数模转换器

MS5208T/MS5208N 是一款 12Bit 、 8 通道输出的电压型 DAC &#xff0c;内部集成上电复位电路、轨到轨输出 Buffer 。接口采用 三线串口模式&#xff0c;最高工作频率可以到 40MHz &#xff0c;兼容 SPI 、 QSPI 、 DSP 接口和 Microwire 串口。输出接到一个轨到轨…

【zabbix】zabbix四大监控方式

zabbix四大监控方式 zabbix四大监控方式1、 Agent2、 SNMP3、IPMI4、JMX 设置 zabbix-snmp 监控 zabbix监控tomcat的jvm内存1.介绍Zabbix Java Gateway 主要功能使用场景 2.Zabbix Java Gateway 配置步骤**3.在被控端的tomcat上开启jvm监控**4.在zabbix-server上添加监控4.1.添…

贾英才主任的中医探索之路

在北京崇文门中医医院&#xff0c;贾英才主任在中医领域的钻研从未停歇。他对药理的探究和药物搭配的研究&#xff0c;展现出了非凡的专注与执着。 贾英才主任常常埋首于浩瀚的中医典籍之中&#xff0c;逐字逐句地研读古代名医的论述&#xff0c;试图从那些古老的智慧中汲取精…

R语言 | 使用ggplot绘制柱状图,在柱子中显示数值和显著性

原文链接&#xff1a;使用ggplot绘制柱状图&#xff0c;在柱子中显示数值和显著性 本期教程 获得本期教程示例数据&#xff0c;后台回复关键词&#xff1a;20240628。&#xff08;PS&#xff1a;在社群中&#xff0c;可获得往期和未来教程所有数据和代码&#xff09; 往期教程…

一文弄懂线性回归模型

1、引言 今天&#xff0c;我们将深入探讨机器学习中的三个关键概念&#xff1a;线性回归、代价函数和梯度下降。这些概念构成了许多机器学习算法的基础。起初&#xff0c;我决定不写一篇关于这些主题的文章&#xff0c;因为它们已经被广泛涉及。不过&#xff0c;我改变了主意&…

安卓开发app-基础的java项目构建补充知识

安卓开发app-基础的java项目构建补充知识&#xff01;上一次分享了基础的项目构建&#xff0c;但是还遗漏了一些基础的内容。今天补充完整。 首先&#xff0c;是关于项目的一些配置文件的信息。 第一个配置文件&#xff1a;{setting.gradle} 国内阿里云仓库地址信息&#xff1…

[leetcode]avoid-flood-in-the-city 避免洪水泛滥

. - 力扣&#xff08;LeetCode&#xff09; class Solution { public:vector<int> avoidFlood(vector<int>& rains) {vector<int> ans(rains.size(), 1);set<int> st;unordered_map<int, int> mp;for (int i 0; i < rains.size(); i) {i…

java周测总结(3)

1、什么是I0流&#xff1f; 是一串流动的字符,从先进先出的方式要求信息的通道。 2、什么是序列化&#xff1f;什么是反序列化&#xff1f; 序例化是将对象的状态存储到特定的存储介质中的过程反序例化是将特定的有合者公质中数据重新构建对象的过程。 3、Java中线程在哪个包下…

CocosCreator构建IOS的wwise教程

CocosCreator构建IOS教程 添加wwise教程: 1.添加include 2.添加SoundEngine 3.添加Profile-iphoneos下面lib下面的.a 4.导入js调用C++的文件 5.导入这些文件 6.初始化ios绝对路径和TTS语音合成对象 6.获得根目录绝对路径,加载pck需要找到绝对路径。怎么找绝对路径? #impor…

【vue3】【vant】 移动端古诗词句子发布收藏项目

更多项目点击&#x1f446;&#x1f446;&#x1f446;完整项目成品专栏 【vue3】【vant】 移动端古诗词句子发布收藏项目 获取源码方式项目说明&#xff1a;其中功能包括素材包含&#xff1a;项目运行环境运行截图 获取源码方式 加Q群&#xff1a;632562109项目说明&#xf…

wavesummit2024发布飞桨3.0版本

今天网上看了wavesummit2024深度学习开发者大会,本来没有啥期待&#xff0c;结果发现飞桨竟然发布3.0版本了&#xff01; 以下是飞桨框架 3.x 的新特性&#xff1a; 动静统一自动并行&#xff1a; 为了降低大模型的编程难度&#xff0c;飞桨还优化了动静统一的半自动并行编程范…

SD-WAN组网对比传统组网的优势

随着商业环境的迅速变化&#xff0c;企业对网络连接的需求不断增长。传统组网方案已经难以满足现代企业的需求&#xff0c;而作为一种新兴的网络解决方案&#xff0c;SD-WAN正逐渐受到企业的青睐。那么&#xff0c;SD-WAN究竟在哪些方面优于传统组网方案呢&#xff1f; 灵活性 …