【前端学习-21】【03】/事件注册、委托、解绑/自动触发事件trigger/事件对象/拷贝对象/多库共存/JQ插件-瀑布流、懒加载、全屏滚动/尺寸、位置操作/链式编程/微博留言/返回顶部

一、事件注册

1.知识点

在这里插入图片描述

2.代码演示:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div {
            width: 200px;
            height: 200px;
        }
        
        .current {
            background-color: pink;
        }
    </style>
    <script src="../JQuery.min.js"></script>
    <script>
        $(function() {
            // // 1.单个事件注册
            // $("div").click(function() {
            //     $(this).css("background", "purple");
            // });
            // $("div").mouseenter(function() {
            //     $(this).css("background", "green");
            // });
            // 2.事件处理on
            // $("div").on({
            //     mouseenter: function() {
            //         $(this).css("background", "yellow");
            //     },
            //     mouseleave: function() {
            //         $(this).css("background", "blue");
            //     }
            // })
            $("div").on("mouseenter mounseleave", function() {
                $(this).toggleClass("current");
            })
        })
    </script>
</head>
<body>
    <div class="current"></div>
</body>
</html>

二、事件委托

在这里插入图片描述

1.知识点

在这里插入图片描述

2.代码演示:

<html lang="en">

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

</head>
<body>
    <ul>
        <li>4</li>
        <li>5</li>
        <li>6</li>
    </ul>
    <ol>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ol>
    <script>
        // 2.on可以实现事件委托
        // $("ul li").click();//以前的写法
        $("ul").on("click", "li", function() {
            alert(11);
        });
        // click是绑定在ul身上的但触发的是li
        // 3.on可以给未来动态创建的元素绑定事件
        // $("ol li").click(function(){
        //     alert(11);
        // })
        $("ol").on("click", "li", function() {
            alert(11);
        });
        var li = $("<li>我是后来创建的</li>");
        $("ol").append(li);
    </script>
</body>
</html>

3.案例:微博发布留言

在这里插入图片描述

<!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
        }
        
        ul {
            list-style: none
        }
        
        .box {
            width: 600px;
            margin: 100px auto;
            border: 1px solid #000;
            padding: 20px;
        }
        
        textarea {
            width: 450px;
            height: 160px;
            outline: none;
            resize: none;
        }
        
        ul {
            width: 450px;
            padding-left: 80px;
        }
        
        ul li {
            line-height: 25px;
            border-bottom: 1px dashed #cccccc;
            display: none;
        }
        
        input {
            float: right;
        }
        
        ul li a {
            float: right;
        }
    </style>
    <script src="JQuery.min.js"></script>
    <script>
        $(function() {
            $(".btn").on("click", function() {
                // 1.点击发布按钮, 动态创建一个小li,放入文本框的内容和删除按钮, 并且添加到ul 中
                var li = $("<li></li>");
                li.html($(".txt").val() + "<a href = 'javascript:;'>删除</a>");
                $("ul").prepend(li);
                li.slideDown();
                $(".txt").val("");
            });
            // 2.点击的删除按钮,可以删除当前的微博留言li
            $("ul").on("click", "a", function() {
                $(this).parent().slideUp(function() {
                    $(this).remove();
                });
            })
        })
    </script>
</head>

<body>
    <div class="box" id="weibo">
        <span>微博发布</span>
        <textarea name="" class="txt" cols="30" rows="10"></textarea>
        <button class="btn">发布</button>
        <ul>
        </ul>
    </div>
</body>

</html>

三、事件解绑off

1.知识点:

在这里插入图片描述在这里插入图片描述
注意:

// 2. one() 但是它只能触发事件一次
    $("p").one("click", function() {
     alert(11);

2.代码演示:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function() {
            $("div").on({
                click: function() {
                    console.log("我点击了");
                },
                mouseover: function() {
                    console.log('我鼠标经过了');
                }
            });
            $("ul").on("click", "li", function() {
                alert(11);
            });
            // 1. 事件解绑 off 
            // $("div").off();  // 这个是解除了div身上的所有事件
            $("div").off("click"); // 这个是解除了div身上的点击事件
            $("ul").off("click", "li");
            // 2. one() 但是它只能触发事件一次
            $("p").one("click", function() {
                alert(11);
            })
        })
    </script>
</head>

<body>
    <div></div>
    <ul>
        <li>我们都是好孩子</li>
        <li>我们都是好孩子</li>
        <li>我们都是好孩子</li>
    </ul>
    <p>我是屁</p>
</body>

</html>

四、自动触发事件trigger()

1.知识点

在这里插入图片描述
默认行为:文本框(焦点获得)

2.代码演示

在这里插入图片描述

<style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function() {
            $("div").on("click", function() {
                alert(11);
            });

   // 自动触发事件
   // 1. 元素.事件()
   // $("div").click();会触发元素的默认行为
   // 2. 元素.trigger("事件")
   // $("div").trigger("click");会触发元素的默认行为
    $("input").trigger("focus");
  // 3. 元素.triggerHandler("事件") 就是不会触发元素的默认行为
  $("div").triggerHandler("click");
  $("input").on("focus", function() {
   $(this).val("你好吗");
  });
   // $("input").triggerHandler("focus");
        });
    </script>
</head>
<body>
    <div></div>
    <input type="text">
</body>

五、JQuery事件对象

1.知识点

在这里插入图片描述

2.代码演示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function() {
            $(document).on("click", function() {
                console.log("点击了document");

            })
            $("div").on("click", function(event) {
                // console.log(event);
                console.log("点击了div");
                event.stopPropagation();
            })
        })
    </script>
</head>
<body>
    <div></div>
</body>
</html

六、JQuery拷贝对象

1. 知识点

在这里插入图片描述在这里插入图片描述

2.代码演示

<script>
  $(function() {
        // var targetObj = {};
        // var obj = {
        //     id: 1,
        //     name: "andy"
        // };
        // // $.extend(target, obj);
        // $.extend(targetObj, obj);
        // console.log(targetObj);
        // var targetObj = {
        //     id: 0
        // };
        // var obj = {
        //     id: 1,
        //     name: "andy"
        // };
        // // $.extend(target, obj);
        // $.extend(targetObj, obj);
        // console.log(targetObj); // 会覆盖targetObj 里面原来的数据
        var targetObj = {
            id: 0,
            msg: {
                sex: '男'
            }
        };
        var obj = {
            id: 1,
            name: "andy",
            msg: {
                age: 18
            }
        };
        // // $.extend(target, obj);
        // $.extend(targetObj, obj);
        // console.log(targetObj); // 会覆盖targetObj 里面原来的数据
        // // 1. 浅拷贝把原来对象里面的复杂数据类型地址拷贝给目标对象
        // targetObj.msg.age = 20;
        // console.log(targetObj);
        // console.log(obj);
        // 2. 深拷贝把里面的数据完全复制一份给目标对象 如果里面有不冲突的属性,会合并到一起 
        $.extend(true, targetObj, obj);
        // console.log(targetObj); // 会覆盖targetObj 里面原来的数据
        targetObj.msg.age = 20;
        console.log(targetObj); // msg :{sex: "男", age: 20}
        console.log(obj);
    })
</script>

七、JQuery多库共存

1.知识点

在这里插入图片描述

2.代码演示

在这里插入图片描述

八、JQuery插件

1.瀑布流

在这里插入图片描述

2.图片懒加载

在这里插入图片描述

3.全屏滚动

在这里插入图片描述

九.Bootstrap JS插件

在这里插入图片描述
注意:
bootstrap插件使用时记得要引入所有的,包括Jquery。
同时要放在container容器里面。

`<div class="container"> </div>`

案例:todolist

1.分析:

(主要利用本地存储)
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

本地存储代码演示:

<script>
        var todolist = [{
            title: '我今天吃八个馒头',
            done: false
        }, {
            title: '我今天学习jq',
            done: false
        }, ];
        // localStorage.setItem("todo", todolist);
        // 1. 本地存储里面只能存储字符串的数据格式 把我们的数组对象转换为字符串格式 JSON.stringify()
        localStorage.setItem("todo", JSON.stringify(todolist));
        var data = localStorage.getItem("todo");
        // console.log(typeof data);
        // console.log(data[0].title);
        // 2. 获取本地存储的数据 我们需要把里面的字符串数据转换为 对象格式 JSON.parse()
        data = JSON.parse(data);
        console.log(data);
        console.log(data[0].title);
    </script>

十、JQuery尺寸、位置操作

1.知识点

在这里插入图片描述

2.代码演示:

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

3.案例:带有动画的返回顶部

在这里插入图片描述在这里插入图片描述在这里插入图片描述

十一、链式编程

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值