web实验4

文章展示了如何利用jQuery对HTML表格进行美化,包括奇偶行背景色设置,点击选中行功能,并详细解释了如何实现数据筛选和级联选择框的功能。同时,还给出了添加和删除表格行的示例代码,强调了动态元素事件处理的方法。
摘要由CSDN通过智能技术生成

奇数行背景色:#FFF38F;偶数行背景色:#FFFFEE;选中行:#FF6500

选中行时,设置单选框选中状态

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jquery.min.js"></script>
    <style>
        table {
            width: 320px;
            border: 1px solid black;
            text-align: center;
            border-collapse: collapse;

        }

        th,
        td {
            border: 1px solid black;
            padding: 5px;
        }

        .odd {
            background-color: #FFF38F;
        }

        .even {
            background-color: #FFFFEE;
        }

        .choose {
            background-color: #FF6500;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <th></th>
            <th>姓名</th>
            <th>性别</th>
            <th>暂住地</th>
        </thead>

        <tbody>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>张三</td>
                <td>男</td>
                <td>四川成都</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>张三</td>
                <td>男</td>
                <td>四川成都</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>张三</td>
                <td>男</td>
                <td>四川成都</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>张三</td>
                <td>男</td>
                <td>四川成都</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id=""></td>
                <td>张三</td>
                <td>男</td>
                <td>四川成都</td>
            </tr>

        </tbody>
    </table>

    <script>
        $(function () {
            $('tbody>tr:odd').addClass('odd')
            $('tbody>tr:even').addClass('even')
            $('tbody tr').click(function () {
                $(this).addClass('choose')
                $(this).siblings().removeClass('choose')
                $(this).children('td').children('input').prop('checked', true)
            })
        })
    </script>
</body>

</html>
  • Jquery中提供了filter方法进行过滤,如$("选择器").filter(内容过滤器)
  • 搜索过程中可先隐藏hide所有数据行,满足条件的行进行显示show()
  • :contains(text) 匹配包含给定文本的元素
  • :empty 匹配所有不包含子元素或者文本的空元素
  • :parent 匹配含有子元素或者文本的元素
  • :has(selector) 匹配含有选择器所匹配的元素的元素
  • <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="./jquery.min.js"></script>
    
        <style>
            * {
                padding: 0;
                margin: 0;
            }
    
            body {
                margin: 100px;
            }
    
            table {
                width: 400px;
                border-collapse: collapse;
                border: 1px solid black;
                margin-top: 50px;
                text-align: center;
            }
    
            th,
            td {
                border: 1px solid black;
            }
    
            th {
                height: 30px;
            }
        </style>
    </head>
    
    <body>
        查询:<input type="text">
        <table>
            <thead>
                <th>姓名</th>
                <th>性别</th>
                <th>暂住地</th>
            </thead>
            <tbody>
                <tr>
                    <td class="name">李四</td>
                    <td>男</td>
                    <td>四川成都</td>
                </tr>
                <tr>
                    <td class="name">张三</td>
                    <td>男</td>
                    <td>四川成都</td>
                </tr>
                <tr>
                    <td class="name">张三</td>
                    <td>男</td>
                    <td>四川成都</td>
                </tr>
                <tr>
                    <td class="name">张三</td>
                    <td>男</td>
                    <td>四川成都</td>
                </tr>
                <tr>
                    <td class="name">张三</td>
                    <td>男</td>
                    <td>四川成都</td>
                </tr>
                <tr>
                    <td class="name">张三</td>
                    <td>男</td>
                    <td>四川成都</td>
                </tr>
            </tbody>
        </table>
    
        <script>
            $(function () {
                $('tbody>tr:even').css('backgroundColor', '#FFF38F')
                $('tbody>tr:odd').css('backgroundColor', '#FFFFEE')
                $('input').on('input', function () {
                    $('.name').parent().hide()
                    let txt = $('input').val()
                    $('.name').filter(`:contains(${txt})`).parent().show()
                })
            })
    
    
        </script>
    </body>
    
    </html>

    级联选择框

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="./jquery.min.js"></script>
    </head>
    
    <body>
        <select id="province">
            <option>请选择</option>
        </select>
        <select id="cities">
            <option>请选择</option>
        </select>
    
        <script>
            $(function () {
                const prov = ['河北省', '辽宁省', '四川省']
                const city = [["石家庄", "邯郸", "唐山", "张家口", "廊坊"], ["沈阳市", "大连市", "鞍山市", "抚顺市", "本溪市"], ['成都', '绵阳', '眉山', '乐山']]
    
                //遍历prov数组,将内容填充进去
                for (let i = 0; i < prov.length; i++) {
                    $('#province').append(`<option>${prov[i]} </option>`)
                }
                $('#province').change(function () {
                    //将不是请选择的内容移除
                    $('#cities').children().not(':eq(0)').remove();
                    // $('#cities').empty()
                    //得到选择的省对应的序号
                    const index = $(this).children('option:selected').index()
                    // console.log(index);
                    const newcity = city[index - 1]
                    // console.log(newcity);
                    for (let i = 0; i < newcity.length; i++) {
                        $('#cities').append(`<option>${newcity[i]} </option>`)
    
                    }
                })
            })
    
        </script>
    </body>
    
    </html>

    数据添加行,delete删除

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="./jquery.min.js"></script>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
    
            body {
                margin: 50px;
                width: 700px;
            }
    
            .top {
                width: 100%;
            }
    
            h2 {
                text-align: center;
                margin-bottom: 20px;
            }
    
            input {
                width: 160px;
                margin-right: 10px;
            }
    
            button {
                display: block;
                margin: 20px auto;
                width: 50px;
                height: 20px;
            }
    
            table {
                width: 400px;
                margin: 50px auto;
                border-collapse: collapse;
            }
    
            th,
            td {
                width: 50px;
                border: 1px solid black;
                text-align: center;
            }
        </style>
    </head>
    
    <body>
        <div class="top">
            <h2>添加新员工</h2>
            name:<input type="text" class="name">
            email:<input type="text" class="email">
            salary:<input type="text" class="salary">
            <button>submit</button>
        </div>
        <hr>
    
        <div class="box">
            <table>
                <thead>
                    <th>name</th>
                    <th>email</th>
                    <th>salary</th>
                    <th></th>
                </thead>
                <tbody>
                    <tr>
                        <td>tom</td>
                        <td>tom@tom.com</td>
                        <td>5000</td>
                        <td><a href="javascript:;">delete</a></td>
                    </tr>
                </tbody>
            </table>
        </div>
    
        <script>
            $(function () {
                $('button').on('click', function () {
                    $('table').append(`<tr> <td>${$('.name').val()}</td> <td>${$('.email').val()}</td> <td>${$('.salary').val()}</td><td><a href="javascript:;">delete</a></td>`)
                    $('input').val('')
                })
                $('table').on('click', 'a', function () {
                    $(this).parents('tr').remove()
                })
                //动态创建的元素进行操作时,需要将事件绑定在已经静态存在的父级上
                // $('a').on('click',function () {
                //     $(this).parent().parent().remove()
                // })
            })
    
        </script>
    </body>
    
    </html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值