前端学习 DAY07

JQuery

JQuery引用
辅助开发javaScript的库
方便处理HTML, 事件, 动画,异步交互
1.本地引用

<head>
    <title>Document</title>
    <script  src="js/jquery-3.6.0.,im.js"></script>
</head>
<body> 
</body>

2.cdn引用
百度 新浪 谷歌

JQuery语法
基础语法

$(selector).action()

美元符号定义JQuery
选择符(selection)查询html页面元素
action()执行对查询的元素操作

文档就绪
作用:文档完全加载完毕以后,在执行JQuer代码
写法:

$(document).ready(funtion(){
           //写JQuery代码
});
$(function(){
       //写JQuery代码
});

元素选择器

<!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>元素选择器</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            // 元素选择器
            // 点击按钮实现 p元素隐藏
            $("button").click(function(){
                $("p").hide();
            });
        });
    </script>
</head>
<body>
    <h1>标题</h1>
    <p>段落1</p>
    <p>段落2</p>
    <button>点击一下</button>
</body>
</html>

id选择器

<!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>id选择器</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            // id选择器
            // 点击按钮实现 p元素隐藏
            $("button").click(function(){
                $("#p2").hide();
            });
        });
    </script>
</head>
<body>
    <h1>标题</h1>
    <p>段落1</p>
    <p id="p2">段落2</p>
    <button>点击一下</button>
</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>类选择器</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            // 类选择器
            // 点击按钮实现 p元素隐藏
            $("button").click(function(){
                $(".test").hide();
                
                // CSS选择器
                // $("p").css("color", "blue");
                $("p").css({
                    "color":"red",
                    "font-size":"50px"
                })
            });
        });
    </script>
</head>
<body>
    <h1>标题</h1>
    <p>段落1</p>
    <p class="test">段落2</p>
    <p class="test">段落3</p>
    <p>段落4</p>
    <button>点击一下</button>
</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>选择器</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
          $(document).ready(function(){
            $("button").click(function(){
                // 全元素选择器
                // $("*").hide();

                // 当前元素选择器
                // $(this).hide();

                // 选取第一个p元素
                // $("p:first").hide();

                // 选取具有某个属性的元素
                // $("[href]").hide();
                
            });
        });
    </script>
</head>
<body>
    <h1>标题</h1>
    <p>段落1</p>
    <p class="test">段落2</p>
    <p class="test">段落3</p>
    <p>段落4</p>
    <a href="https://www.baidu.com">百度一下</a>
    <button>点击一下</button>
</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>事件</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(function(){
            // 鼠标穿过元素触发
            // $("#p1").mouseenter(function(){
            //     console.log("1234");
            // });

            // 鼠标离开元素触发 mouseleave

            // 鼠标悬停
            $("#p1").hover(function(){
                alert("悬停在p元素上!");
            });  

            // jquery设置监听的时候 只需要在选择的元素后直接调用相应的监听方法即可。

        });
    </script>
</head>
<body>
    <p id="p1">我是段落、、、</p>
</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=\, initial-scale=1.0">
    <title>隐藏和显示</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#hide").click(function(){
                $("#p").hide();
            });
            $("#show").click(function(){
                $("#p").show();
            });
            // 两个方法的合体 toggle
            $("#toggle").click(function(){
                // 显示隐藏的元素,隐藏显示的元素
                $("#p").toggle();
            });
        });
    </script>
</head>
<body>
    <p id="p">点击按钮显示或隐藏</p>
    <button id="hide">隐藏</button>
    <button id="show">显示</button>
    <button id="toggle">隐藏和显示</button>
</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=\, initial-scale=1.0">
    <title>淡入和淡出</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <style>
        div{
            width: 80px;
            height: 80px;
            background-color: aqua;
            display: none;
        }
    </style>
    <script>
        $(document).ready(function(){
            $("#fadein").click(function(){
                // 淡入元素
                $("#div1").fadeIn(1000);
            });
            $("#fadeout").click(function(){
                // 淡出元素
                $("#div1").fadeOut(1000);
            });
            // 结合上面两个方法  淡入或淡出
            $("#fadetoggle").click(function(){
                // 淡出元素
                $("#div1").fadeToggle(1000);
            });
        });
    </script>
</head>
<body>
    <button id="fadein">点击淡入元素</button>
    <button id="fadeout">点击淡出元素</button>
    <button id="fadetoggle">点击淡入/淡出元素</button>
    <div id="div1"></div>
</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=\, initial-scale=1.0">
    <title>滑动</title>
    <script src="js/jquery-3.6.0.min.js"></script>

    <script>
      $(function(){
        $("#btn1").click(function(){
            $("#div1").slideDown("slow");
        });
        $("#btn2").click(function(){
            $("#div1").slideUp("fast");
        });
        // 结合上面两种方式 滑下和滑上
        $("#btn3").click(function(){
            $("#div1").slideToggle(1000);
        });
      });
    </script>

    <style>
        div{
            width: 200px;
            height: 100px;
            background-color: blue;
            display: none;
        }
    </style>
</head>
<body>
    <button id="btn1">点击一下</button>
    <button id="btn2">点击一下</button>
    <button id="btn3">点击一下</button>
    <div id="div1"></div>
</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>动画</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        // 设置动画的元素 需要css中设定position属性
        $(function(){
            $("#btnstart").click(function(){
                // 使用多个animate  逐一执行
                $("div").animate({
                    left:'250px',
                    opacity:"0.3",
                    height:"200px",
                    width:"200px"
                });
                $("div").animate({
                    left:'8px',
                    opacity:"1",
                    height:"100px",
                    width:"100px"
                });
            });
            // 停止动画
            $("#btnstop").click(function(){
                // 默认是false 后续队列动画还会执行
                $("div").stop(true);
            });
        })
    </script>

    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
        }
    </style>
</head>

<body>
    <button id="btnstart">开始动画</button>
    <button id="btnstop">停止动画</button>
    <div></div>
</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>方法链接</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").css("color", "red").fadeOut(1000).fadeIn(1000);
            });
        });
    </script>
</head>
<body>
    <p>我是一个段落!</p>
    <button>点击一下</button>
</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>设置内容</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(function(){
            // 设置内容
            $("#btn1").click(function(){
                $("#p1").text("<b>今天天气晴朗!</b>");
            });
            $("#btn2").click(function(){
                $("#p2").html("<b>今天天气晴朗!</b>");
            });
            $("#btn3").click(function(){
                $("input").val("今天天气晴朗!");
            });
            // 设置属性 可同时修改多个属性
            $("#btn4").click(function(){
                $("a").attr({
                    "href":"https://www.taobao.com"
                });
            });
        });
    </script>
</head>
<body>
    <p id="p1">这是段落1</p>
    <p id="p2">这是段落2</p>
    姓名:<input type="text" value="张三">
    <br>
    <a href="https://www.baidu.com">百度一下</a>
    <br>
    <button id="btn1">设置文本</button>
    <button id="btn2">设置html</button>
    <button id="btn3">设置值</button>
    <button id="btn4">设置属性</button>
</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>添加元素</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(function(){
           $("#btn1").click(function(){
                // 添加元素 追加 从末尾位置添加
                // $("ol").append("<li>西瓜</li>");

                // 从头部开始添加
                // $("ol").prepend("<li>西瓜</li>");

                // before 元素前添加
                $("ol").before("<b>之前添加</b>");
                // after 元素后添加
                $("ol").after("<b>之后添加</b>");
           });
        });
    </script>
</head>
<body>
    
    <ol>
        <li>苹果</li>
        <li>香蕉</li>
        <li>橘子</li>
    </ol>
    <button id="btn1">添加元素</button>
</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>添加元素</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(function(){
           $("#btn1").click(function(){
                // 删除被选元素及其子元素 
                // 可以添加过滤条件
                 $("li").remove(".li");

                // 删除被选元素的子元素
                // $("ol").empty();
           });  
        });
    </script>
</head>
<body>
    
    <ol>
        <li id="li1">苹果</li>
        <li class="li">香蕉</li>
        <li>橘子</li>
    </ol>
    <button id="btn1">删除元素</button>
</body>
</html>

操作css

<!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>操作css</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#add").click(function(){
                // 添加css样式
                $("div").addClass("col");
                $("div").addClass("col2");
                // 移除css样式
                
            });

            $("#remove").click(function(){
                // 移除css样式
                $("div").removeClass("col2");
                
            });

            // toggleClass 结合上面两种方式 
            $("#toggle").click(function(){
                // 移除css样式
                $("div").toggleClass("col2");
                
            });


        });
    </script>

    <style>
        .col{
            width: 100px;
            height: 100px;
            background-color: chartreuse;
        }

        .col2{
            color: aqua;
        }
    </style>
</head>
<body>
    <div>div</div>
    <button id="add">点击一下</button>
    <button id="remove">移除css样式</button>
    <button id="toggle">移除css样式</button>
</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>尺寸信息</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
          var d = $("div");  
          // 元素的宽和高
          console.log(d.width());
          console.log(d.height());
          // 包含内边距的宽和高
          console.log(d.innerWidth());
          console.log(d.innerHeight());
         // 包含内边距和边框的 宽和高
          console.log(d.outerWidth());
          console.log(d.outerHeight());
        });
    </script>
</head>
<body>
   <div style="height: 200px;width:100px;background-color: aqua; padding: 10px; margin: 20px;border: 5px solid black;"></div>
</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>遍历</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <style>
        .div2{
           border: 2px solid lightgray;
           padding: 10px;
           margin: 10px;
           display: block;
           width: 500px;
       }
       
       
   </style>

    <script>
        $(document).ready(function(){
            // 获取选中元素的父元素
            // $("span").parent().css({"color":"red","border":"2px solid red"});

            // 获取选中元素的父元素 所有祖先全部获取到 一直到根元素
          //  $("span").parents().css({"color":"red","border":"2px solid red"});
            // 添加过滤条件 不仅是祖先元素并且是ul元素
            //$("span").parents("ul").css({"color":"red","border":"2px solid red"});

            // parentsUntil 返回的是两个元素之间的所有祖先元素
            // $("span").parentsUntil("div").css({"color":"red","border":"2px solid red"});

            // 获取选中元素的子元素
           //$("#div").children().css({"color":"red","border":"2px solid red"});
           // 选取选中元素后代元素   * 所有后代元素
           //$("#div").find("span").css({"color":"red","border":"2px solid red"});

           // 获取兄弟元素 返回所有被选元素的兄弟元素
            // $("p").siblings().css({"color":"red","border":"2px solid red"});
            // 添加过滤条件
            //$("p").siblings("h2").css({"color":"red","border":"2px solid red"});

            // 被选元素的下一个兄弟元素
            // $("h2").next().css({"color":"red","border":"2px solid red"});
            // nextall 被选元素的所有跟随的兄弟元素
            //$("p").nextAll().css({"color":"red","border":"2px solid red"});

            // nextUntil  介于两个元素之间的所有兄弟元素
            // $("p").nextUntil("h3").css({"color":"red","border":"2px solid red"});

            // first 被选元素的第一个元素
            // div内部的第一个p元素
            //$("div p").first().css({"color":"red","border":"2px solid red"});
            // div内部的最后一个p元素
            //$("div p").last().css({"color":"red","border":"2px solid red"});

            // 返回指定索引的元素 下标从0开始
            // $("p").eq(2).css({"color":"red","border":"2px solid red"});

            // filter
            // $("p").filter(".p").css({"color":"red","border":"2px solid red"});

            // not 与 filter正好相反的
            $("p").not(".p").css({"color":"red","border":"2px solid red"});
        });
    </script>

   
</head>
<body>

    <div class="div2" id="div">div祖父元素
        <p id="p1">p父元素
            <span>span</span>
        </p>
        <p class="p">p父元素
            <span>span</span>
        </p>
        <h2>标题2</h2>
        <h3>标题3</h3>
        <p class="p">p父元素
            <span>span</span>
        </p>
    </div>

   
</body>
</html>

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>选择器</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script>
          $(document).ready(function(){
         
        });
    </script>
</head>
<body>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值