Web前端平时练习

平时练习四

1. 使用jquery或vue实现如下界面效果:

示例
提示:
奇数行背景色:#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>E4.1</title>
</head>
<body>
    <table>
        <tr>
            <td></td>
            <td class="name">姓名</td>
            <td>性别</td>
            <td>暂住地</td>
        </tr>
        <tr>
            <td> <input type="radio" name="select"> </td>
            <td class="name">张三</td>
            <td>男</td>
            <td>四川成都</td>
        </tr>
        <tr>
            <td> <input type="radio" name="select"> </td>
            <td class="name">李四</td>
            <td>女</td>
            <td>四川绵阳</td>
        </tr>
        <tr>
            <td> <input type="radio" name="select"> </td>
            <td class="name">王五</td>
            <td>男</td>
            <td>四川南充</td>
        </tr>
        <tr>
            <td> <input type="radio" name="select"> </td>
            <td class="name">赵六</td>
            <td>男</td>
            <td>四川自贡</td>
        </tr>
        <tr>
            <td> <input type="radio" name="select"> </td>
            <td class="name">陈勇</td>
            <td>男</td>
            <td>四川德阳</td>
        </tr>
        <tr>
            <td> <input type="radio" name="select"> </td>
            <td class="name">罗梅</td>
            <td>女</td>
            <td>四川宜宾</td>
        </tr>
    </table>

</body>
</html>

<script src="./jquery-3.6.4.js"></script>

<script>

    $("tr:odd").css("background", "#FFF38F");

    $("tr:even").css("background", "#FFFFEE");

    $("tr:eq(0)").css({"background":"white", "font-weight": "bold"});

    $("tr").click(function(){
        // console.log(this);
        $(this).find("input:radio").prop("checked","true");
        $("tr:odd").css({"background":"#FFF38F", "color": "black"});
        $("tr:even").css({"background":"#FFFFEE", "color": "black"});
        $("tr:eq(0)").css({"background":"white", "color": "black"});

        $(this).css({"background":"#FF6500", "color": "white"});
    });
</script>

<Style>

    table{
        border-collapse: collapse;    
    }

    td{
        border: 1px solid black;
    }

    tr > td:nth-child(1){
        text-align: center;
        width: 60px;
    }

    tr > td:nth-child(2){
        text-align: center;
        width: 60px;
    }

    tr > td:nth-child(3){
        text-align: center;
        width: 60px;
    }

    tr > td:nth-child(4){
        text-align: center;
        width: 100px;
    }

</Style>


2.使用jquery或vue实现内容过滤,效果如下图所示:

示例
提示:
Jquery中提供了filter方法进行过滤,如$(“选择器”).filter(内容过滤器)
搜索过程中可先隐藏hide所有数据行,满足条件的行进行显示show()

代码:

<!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>E4.2</title>
</head>
<body>

    <div>
        <label>查询:</label>
        <input id="input">
    </div>

    <table>
        <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">Rain</td>
            <td>男</td>
            <td>浙江杭州</td>
        </tr>
        <tr>
            <td class="name">MAXMAN</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>
    </table>

</body>
</html>

<script src="./jquery-3.6.4.js"></script>

<script>

$(function(){
        $("tr:odd").css("background", "#FFF38F");

        $("tr:even").css("background", "#FFFFEE");

        $("tr:eq(0)").css({"background":"white", "font-weight": "bold"});

        $("div > input").bind("input propertychange",function(event){
        let inputMessage = $("div > input").val();

        $("tr").filter("tr:nth-child(1) ~ tr").hide();
        
        $("tr").each(function(){
            console.log(inputMessage);
            console.log(1);
            var text = $(this).children("td:first").text();
            console.log(text);
            
            if (text.includes(inputMessage))
            {
                $(this).show();
            }
            
        });
    });
});

</script>

<Style>

    div{
        line-height: 50px;
    }

    div > input{
        border: 1px solid black;
    }

    table{
        border-collapse: collapse;    
    }

    td{
        border: 1px solid black;
    }

    tr > td:nth-child(1){
        text-align: center;
        width: 150px;
    }

    tr > td:nth-child(2){
        text-align: center;
        width: 60px;
    }

    tr > td:nth-child(3){
        text-align: center;
        width: 100px;
    }

</Style>


3.使用 JQuery或vue 实现级联选择框,界面实现效果参考如下图。

示例

代码:

<!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>E4.3</title>
</head>
<body>
    <div>
        <select id="selectProvince">
            <option value="">请选择</option>
        </select>
        <select id="selectCity">
            <option value="">请选择</option>
        </select>        
    </div>
</body>
</html>

<script src="./jquery-3.6.4.js"></script>

<script>
    var provinceTag;
    var province = ['河北省','辽宁省','山东省'];
    var city =[['石家庄', '邯郸', '唐山', '张家口', '廊坊'],
                ['沈阳市', '大连市', '抚顺市', '辽阳市'],
                ['济南市', '临沂市', '莱芜市', '菏泽市']];

    $(function() {
        for(var i=0;i<province.length;i++){
            $('#selectProvince').append('<option>'+province[i]+'</option>');
        }

        $('#selectProvince').change(function(){
            $('#selectCity').children().not(':eq(0)').remove();
            provinceTag = $(this).children('option:selected').index();
            var subCity = city[provinceTag-1];
            for(var j=0;j<subCity.length;j++){
                $('#selectCity').append('<option>'+subCity[j]+'</option>');
            }
         })
    })
</script>


4.使用Jquery或vue实现将输入数据添加至表格中,通过点击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>E4.4</title>
</head>
<body>
    
    <form>
        <fieldset>
            <label id="main">添加新员工</label>
            <br>
            <label>name:  <input id="name"   type="text"/></label>
            <label>email: <input id="email"  type="text"/></label>
            <label>salary:<input id="salary" type="text"/></label>
            <br>
            <input id="submit" type="button" value="Submit"/>
        </fieldset>
    </form>

    <table>
        <thead>
            <tr>
                <th style="width: 60px;">Name</th>
                <th style="width: 140px;">Email</th>
                <th style="width: 62px;">Salary</th>
                <th style="width: 65px;"></th>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td>Tom</td>
                <td>tom@tom.com</td>
                <td>5000</td>
                <td class="deleteBtn">Delete</td>
            </tr>
            <tr>
                <td>Jerry</td>
                <td>jerry@sohu.com</td>
                <td>8000</td>
                <td class="deleteBtn">Delete</td>
            </tr>
            <tr>
                <td>Bob</td>
                <td>bob@tom.com</td>
                <td>10000</td>
                <td class="deleteBtn">Delete</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

<script src="./jquery-3.6.4.js"></script>

<script>
    $(function(){

        $("td.deleteBtn").click(function(){
                $(this).parents("tr").remove();
        });

        $("#submit").click(function(){
            if (($("#name").val().trim() != "") && ($("#email").val().trim() != "") && ($("#salary").val().trim() != "")){
                let name   = $("#name").val();
                let email  = $("#email").val();
                let salary = $("#salary").val();

                $("table").append("<tr><td>" +name + "</td><td>" +email + "</td><td>" +salary + "</td>" +"<td class='del'>Delete</td></tr>");
                $("td:last").prop("class", "deleteBtn");
                $("td:last").css({"text-decoration":"underline", "color":"blue", "border":"2px solid grey", "padding":"5px",});
            }
            
            $("td.deleteBtn").click(function(){
                $(this).parents("tr").remove();
        });

        });

    });
</script>

<Style>
    #main{
        font-weight: bold;
    }

    fieldset{
        border: 0px;
        text-align: center;
        border-bottom: 1px solid black;
    }
    
    input{
        margin-top: 30px;
        margin-bottom: 10px;
    }

    #submit{
        border: 1px solid #4e7cb3;
    }
    
    table{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        border-collapse: collapse;
        width: 330px;
        text-align: center;
    }
    
    td, th{
        border: 2px solid grey;
        padding: 5px;
    }
    
    .deleteBtn{
        text-decoration: underline;
        color: blue;
    }
</Style>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值