Day02-jQuery

一.事件处理

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./js/jquery-3.6.0.min.js"></script>
    <style>
        .box {
            width: 200px;
            height: 200px;
            background-color: pink;
        }

        .son {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>

    <div class="box">
        <div class="son"></div>
    </div>

    <button>手动触发</button>

    <script>

        // $(".box").click(function () {
        //     console.log(1);
        // })

        // on()
        // 可绑定多个相同的事件
        // $(".box").on("click",function(){
        //     console.log("abc");
        // });

        $(".box").on("click", {
            name: "张三",
            age: 18
        }, function (e) {
            console.log("ABC");
            console.log(e);
            console.log(e.data);
        })

        // $(".box").on({
        //     click: function () {
        //         console.log("hello");
        //     },
        //     mouseenter: function () {
        //         console.log("world");
        //     }
        // })

        $(".box").on("click", ".son", function () {
            console.log("son事件");
        })

        // $(".son").click(function(){
        //     console.log("son事件");
        // })

        $(".box").append($("<div class='son'></div>"))


        // one()
        // $(".box").one("click",function(){
        //     console.log("昙花一现");
        // })


        // trigger()
        $("button").click(function () {
            console.log("手动触发");
        }).trigger("click");

        // hover()
        $(".box").hover(function () {
            console.log("进入");
        }, function () {
            console.log("移出");
        })

    </script>

</body>

</html>

二.动画效果

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 800px;
            height: 400px;
            background-color: pink;
            position: absolute;
        }
    </style>

    <script src="./js/jquery-3.6.0.min.js"></script>
</head>

<body>

    <button class="show">显示</button>
    <button class="hide">隐藏</button>
    <button class="toggle">显示隐藏切换</button>
    <button class="fadein">淡入</button>
    <button class="fadeout">淡出</button>
    <button class="fadeto">指定透明度</button>
    <button class="fadeToggle">淡入淡出切换</button>
    <button class="slideDown">向下展开</button>
    <button class="slideUp">向上卷起</button>
    <button class="slideToggle">展开收起切换</button>
    <button class="animate">自定义动画</button>
    <button class="stop">淡入淡出切换</button>

    <div class="box">
        <p>下周气温骤降</p>
        <p>大家注意保暖</p>
    </div>

    <script>

        $(".show").click(function () {
            $(".box").show(2000, "linear", function () {
                console.log("动画执行完毕");
            })
        })

        $(".hide").click(function () {
            $(".box").hide(2000, "linear", function () {
                console.log("动画执行完毕");
            })
        })

        $(".toggle").click(function () {
            $(".box").toggle(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".fadein").click(function () {
            $(".box").fadeIn(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".fadeout").click(function () {
            $(".box").fadeOut(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".fadeto").click(function () {
            $(".box").fadeTo(2000, 0.5, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".fadeToggle").click(function () {
            $(".box").fadeToggle(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".slideDown").click(function () {
            $(".box").slideDown(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".slideUp").click(function () {
            $(".box").slideUp(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".slideToggle").click(function () {
            $(".box").slideToggle(2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        $(".animate").click(function () {
            $(".box").animate({
                width: 600,
                height: 300,
                top: 50,
                left: 100
            }, 2000, "swing", function () {
                console.log("动画执行完毕");
            })
        })

        // 停止动画排队 stop()
        $(".stop").click(function(){
            $(".box").stop().slideToggle(2000,"swing",function(){
                console.log("动画执行完毕");
            })
        })

    </script>

</body>

</html>

三.导航栏

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        ul {
            display: flex;
        }

        li {
            width: 100px;
            height: 30px;
            box-sizing: border-box;
            position: relative;
            border: 1px solid #ccc;
            text-align: center;
            line-height: 30px;
        }

        .box {
            position: absolute;
            width: 100px;
            height: 150px;
            background-color: red;
            top: 30px;
            left: 0;
            display: none;
        }
    </style>

    <script src="./js/jquery-3.6.0.min.js"></script>
</head>

<body>

    <ul>
        <li>首页
            <div class="box">1</div>
        </li>
        <li>产品
            <div class="box">2</div>
        </li>
        <li>我的
            <div class="box">3</div>
        </li>
    </ul>

    <script>

        $("li").hover(function () {
            $(this).children().stop().slideDown(1000, "linear", function () {
                console.log("over");
            })
        }, function () {
            $(this).children().stop().slideUp(1000, "linear", function () {
                console.log("over");
            })
        })

    </script>

</body>

</html>

四.$each

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/jquery-3.6.0.min.js"></script>
</head>

<body>

    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>

    <script>

        var arr = [10, 20, 30, 40, 50];

        $("div").each(function (index, value) {
            console.log(value);
        })


        $.each(arr, function (index, value) {
            console.log(index);
            console.log(value);
        })

        var obj = {
            name: "zhangsan",
            age: 18
        }
        $.each(obj, function (key, value) {
            console.log(key);
            console.log(value);
        })

        var rel = $.map(arr, function (value, index) {
            return value + 1;
        })
        console.log(rel);

    </script>

</body>

</html>

五.$extend

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./js/jquery-3.6.0.min.js"></script>
</head>

<body>

    <script>

        var obj = {
            name: "xiaohao",
            age: 20,
            gender: "男"
        }

        var newobj = {}

        $.extend(true, newobj, obj);
        console.log(newobj);

    </script>

</body>

</html>

六.jQuery中的请求

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./js/jquery-3.6.0.min.js"></script>
</head>

<body>

    <script>

        var baseUrl = "http://localhost:3008";

        $.get(baseUrl + "/api/student/getStudent", { name: "周杰" }, function (data, status, xhr) {
            console.log(data);
            console.log(status);
            console.log(xhr);
        })

        $.post(baseUrl + "/api/student/addStudent", {
            clazz: "火花239",
            name: "胡定海",
            gender: "男",
            age: "20",
            tel: "13999999999",
            address: "信阳",
            remark: "好谷吴彦祖"
        }, function (data) {
            console.log(data);
        })

    </script>

</body>

</html>

七.$ajax

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./js/jquery-3.6.0.min.js"></script>
</head>

<body>

    <script>

        // 原生js中ajax怎么写?
        // 1.创建对象
        // var xhr = new XMLHttpRequest();

        // // 2.发起请求
        // xhr.open("GET/POST", "URL");

        // // 3.设置请求头
        // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        // xhr.setRequestHeader("Content-Type", "application/json");

        // // 4.发送请求
        // xhr.send(data);

        // // 5.监听状态变化
        // xhr.onreadystatechange = function () {
        //     // 判断状态信息
        //     if (xhr.readyState == 4) {
        //         // 判断是否成功
        //         if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
        //             console.log(xhr.response);
        //         }
        //     }
        // }

        // jquery 中的ajax
        $.ajax({
            // 请求类型
            type: "GET",
            url: baseUrl + "",

            // 携带数据
            data: {},
            success: function (data, status, xhr) {
                console.log("请求成功");
            },
            error: function (error) {
                console.log("请求失败");
                console.log(error);
            }
        })

        $.ajax({
            // 请求类型
            type: "POST",
            url: baseUrl + "",

            // 设置请求头
            contentType: "application/json",

            // 携带数据
            data: {},
            success: function (data, status, xhr) {
                console.log("请求成功");
            },
            error: function (error) {
                console.log("请求失败");
                console.log(error);
            }
        })

    </script>

</body>

</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值