jQuery练习

1、左侧菜单栏
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>left menu</title>
    <style>
        .menu{
            height: 300px;
            width: 30%;
            background: darkseagreen;
            float: left;
        }
        .content{
            height: 300px;
            width: 70%;
            background: coral;
            float: left;
        }
        .title{
            background: darksalmon;
            color: black;
            font-size: larger;
        }
        .hide{
            display: none;
        }
    </style>
</head>
<body>
<div>
    <div class="menu">
        <div class="item">
            <div class="title">菜单一</div>
            <div class="con hide">
                <div>111</div>
                <div>111</div>
                <div>111</div>
            </div>
        </div>

        <div class="item">
            <div class="title">菜单二</div>
            <div class="con hide">
                <div>222</div>
                <div>222</div>
                <div>222</div>
            </div>
        </div>

        <div class="item">
            <div class="title">菜单三</div>
            <div class="con hide">
                <div>333</div>
                <div>333</div>
                <div>333</div>
            </div>
        </div>

    </div>

    <div class="content"></div>

</div>
<script src="jquery-3.5.1.js"></script>
<script>
    $(".item .title").click(function () {
        $(this).next().removeClass("hide").parent().siblings().children(".con").addClass("hide")
    })
</script>
</body>
</html>
2、tab切换
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tab切换</title>
    <style>
        *{
            margin: 0px;
            /*外边距*/
            padding: 0px;
            /*内边距*/
        }
        .outer{
            margin: auto;
            /*使左右外边距平分,居中效果*/
            width: 40%;
        }
        .menu{
            background: darkseagreen;
            line-height: 40px;
            border: solid 1px black;
        }
        .content{
            background: darksalmon;
            border: solid 1px black;
            height: 200px;
        }
        .hide{
            display: none;
        }
        li{
            display: inline-block;
            /*内联块状元素,和其他元素可在一行,可设置元素高度、宽度、行高、边距*/
        }
        .current{
            color: gold;
            background: gray;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="menu">
        <ul>
            <li xxx="c1" class="current" onclick="tab(this)">菜单一</li>
            <li xxx="c2" onclick="tab(this)">菜单二</li>
            <li xxx="c3" onclick="tab(this)">菜单三</li>

        </ul>
    </div>
    <div class="content">
        <div id="c1">内容一</div>
        <div id="c2" class="hide">内容二</div>
        <div id="c3" class="hide">内容三</div>
    </div>
</div>
<script src="jquery-3.5.1.js"></script>
<script>
    function tab(self) {
        var index=$(self).attr("xxx")
        // attr("xxx")获取属性的值
        $(self).addClass("current").siblings().removeClass("current")
        $("#"+index).removeClass("hide").siblings().addClass("hide")
    }
</script>
</body>
</html>
3、checkbox正反选
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<button onclick="select('all')">全选</button>
<button onclick="select('cancel')">取消</button>
<button onclick="select('reverse')">反选</button>
<!--相同函数名通过传递不同参数来选择不同操作-->

<table border="1" id="Table">
    <tr>
        <td><input type="checkbox"></td>
        <td>111</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>222</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>333</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>444</td>
    </tr>
</table>

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

<script>
    function select(choice) {
        if(choice=="all"){
            $("table :checkbox").prop("checked",true)
        }
        else if (choice=="cancel"){
            $("table :checkbox").prop("checked",false)
        }
        else if(choice=="reverse"){
            $("table :checkbox").each(function () {
                $(this).prop("checked",!$(this).prop("checked"))
            })
        }
    }
</script>
</body>
</html>
4、模态对话框
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模态对话框</title>
    <style>
        .back{
            background: burlywood;
            height: 2000px;
        }

        .shade{
            background: aquamarine;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            opacity: 0.5;
        }

        .model{
            position: fixed;
            background: gold;
            top: 50%;
            left: 50%;
            width: 200px;
            height: 200px;
            margin-left: -100px;
            margin-top: -100px;
            /*左边框和上边框偏移100px居中*/
        }

        .hide{
            display: none;
        }

    </style>
</head>
<body>
<div class="back">
    <input type="button" value="点击" onclick="action1(this)">
</div>

<div class="shade hide"></div>

<div class="model hide">
    <input type="button" value="取消" onclick="action2(this)">
</div>

<script src="jquery-3.5.1.js"></script>
<script>
    function action1(self) {
        $(self).parent().siblings().removeClass("hide")
    }
    function action2(self) {
        $(self).parent().addClass("hide").prev().addClass("hide")
    }

</script>
</body>
</html>

5、复制样式条
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>复制样式条</title>
</head>
<body>
<div>
    <div class="item">
        <input type="button" value="+" onclick="add(this)">
        <input type="text">
    </div>
</div>
<script src="jquery-3.5.1.js"></script>
<script>
    function add(self) {
        var $clone_obj=$(self).parent().clone();
        $clone_obj.children(":button").val("-").attr("onclick","remove(this)");
        ($(self)).parent().parent().append($clone_obj);
    }
    function remove(self) {
        $(self).parent().remove()
    }
</script>
</body>
</html>
6、返回顶部
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <style>
        body{
            margin: 0px;
        }
        .div1{
            background: darkgreen;
            overflow: auto;
            width: 300px;
        }
        .div2{
            background: burlywood;
        }
        .div3{
            background: darkseagreen;
        }
        .div{
            height: 500px;
        }
        .returnTop{
            width: 100px;
            height: 100px;
            background: gold;
            text-align: center;
            line-height: 100px;
            right: 0;
            bottom: 0;
            position: fixed;
        }
        .hide{
            display: none;
        }
    </style>
</head>
<body>
<div>
    <div class="div1 div">
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
        <div>hello</div>
    </div>
    <div class="div2 div"></div>
    <div class="div3 div"></div>
    <div class="returnTop hide" onclick="returnTop()">返回顶部</div>
</div>
<script src="jquery-3.5.1.js"></script>
<script>
    window.onscroll=function () {
        var $current=$(window).scrollTop()
        if($current>100)
        {
            $(".returnTop").removeClass("hide")
        }
        else {
            $(".returnTop").addClass("hide")
        }
    }
    function returnTop() {
        $(window).scrollTop(0)
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值