<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>表格操作</title>
    <style>
        .mc {
            border: 2px solid red;
        }
    </style>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var isAdded = false; //是否已经添加过表标题
            var isCheckedAll = false; //全选复选框是否选中
<pre><code>        //添加表标题
        $(&quot;input[value='添加表标题']&quot;).click(function(){
            if(!isAdded){
                $(&quot;#tab1&quot;).before(&quot;&lt;h2&gt;商品列表&lt;/h2&gt;&quot;);
                $(&quot;#tab1&quot;).prev().css(&quot;text-align&quot;,&quot;center&quot;);
                isAdded = true;
            }
        });

        //添加样式
        $(&quot;input[value='添加样式']&quot;).click(function(){
            $(&quot;#tab1&quot;).addClass(&quot;mc&quot;);
        });

        //删除样式
        $(&quot;input[value='删除样式']&quot;).click(function(){
            $(&quot;#tab1&quot;).removeClass(&quot;mc&quot;);
        });

        //切换样式
        $(&quot;input[value='切换样式']&quot;).click(function(){
            $(&quot;#tab1&quot;).toggleClass(&quot;mc&quot;);
        });

        //全选复选框
        $(&quot;th:first input[type='checkbox']&quot;).click(function(){
            if($(this).prop(&quot;checked&quot;)){
                isCheckedAll = true;
                $(&quot;td:first-child input[type='checkbox']&quot;).prop(&quot;checked&quot;,true);
            }else{
                isCheckedAll = false;
                $(&quot;td:first-child input[type='checkbox']&quot;).prop(&quot;checked&quot;,false);
            }
        });

        //行复选框
        $(&quot;td:first-child input[type='checkbox']&quot;).click(function(){
            if($(this).prop(&quot;checked&quot;)){
                if($(&quot;td:first-child input[type='checkbox']:checked&quot;).length == $(&quot;td:first-child input[type='checkbox']&quot;).length){
                    $(&quot;th:first input[type='checkbox']&quot;).prop(&quot;checked&quot;,true);
                    isCheckedAll = true;
                }
            }else{
                $(&quot;th:first input[type='checkbox']&quot;).prop(&quot;checked&quot;,false);
                isCheckedAll = false;
            }
        });

        //添加行
        $(&quot;input[value='添加']&quot;).click(function(){
            var lastId = parseInt($(&quot;#tab1 tr:last-child td:nth-child(2)&quot;).text());
            var newId = lastId + 1;
            var newRow = &quot;&lt;tr&gt;&lt;td&gt;&lt;input type='checkbox'&gt;&lt;/td&gt;&lt;td&gt;&quot; + newId + &quot;&lt;/td&gt;&lt;td&gt;新分类&lt;/td&gt;&lt;td&gt;新描述&lt;/td&gt;&lt;td&gt;&lt;a href=''&gt;删除&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
            $(&quot;#tab1 tr:last-child&quot;).before(newRow);
            if(isCheckedAll){
                $(&quot;td:first-child input[type='checkbox']:last&quot;).prop(&quot;checked&quot;,true);
            }
        });

        //删除行
        $(&quot;#tab1&quot;).on(&quot;click&quot;,&quot;a&quot;,function(){
            $(this).parent().parent().remove();
            if(isCheckedAll){
                if($(&quot;td:first-child input[type='checkbox']:checked&quot;).length == 0){
                    $(&quot;th:first input[type='checkbox']&quot;).prop(&quot;checked&quot;,false);
                    isCheckedAll = false;
                }
            }
        });
    });
&lt;/script&gt;
</code></pre>
</head>
<body>
<table id="tab1" border="1" width="500" align="center">
    <tr><td colspan="5">分类名称:<input type="text"><br>分类描述:<input type="text" /></td></tr>
    <tr>
        <td colspan="5">
            <input type="button" value="添加"/>
            <input type="button" value="添加表标题">
            <input type="button" value="添加样式">
            <input type="button" value="删除样式">
            <input type="button" value="切换样式">
        </td>
    </tr>
    <tr>
        <th><input type="checkbox"></th>
        <th>分类ID</th>
        <th>分类名称</th>
        <th>分类描述</th>
        <th>操作</th>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>1</td>
        <td>手机数码</td>
        <td>手机数码类商品</td>
        <td><a href="#">删除</a></td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>2</td>
        <td>电脑办公</td>
        <td>电脑办公类商品</td>
        <td><a href="#">删除</a></td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>3</td>
        <td>鞋靴箱包</td>
        <td>鞋靴箱包类商品</td>
        <td><a href="#">删除</a></td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>4</td>
        <td>家居饰品</td>
        <td>家居饰品类商品</td>
        <td><a href="#">删除</a></td>
    </tr>
</table>
</body>
</html>

原文地址: https://www.cveoy.top/t/topic/nuHw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录