pink-jQuery

一、jQuery 概述

image-20210717211546864

image-20210717211620284

image-20210717211750328

image-20210717211801193

image-20210717211843467

二、jQuery的基本使用

image-20210717212028057

image-20210717212858178

image-20210717213439878

  $('div').hide();
        //1. 等待页面DOM加载完毕再去执行js 代码
        // $(document).ready(function() {
        //     $('div').hide();
        // })
        // 2. 等待页面DOM加载完毕再去执行js 代码
        $(function(){
            $('div').hide();
        })

image-20210717213902842

image-20210717214414185

image-20210717214329577

image-20210717214934928

image-20210717214941874

三、jQuery常用API

image-20210717215055068

1.jQuery选择器

image-20210717215616426

image-20210717215824234

image-20210717215949273

image-20210717220245420

image-20210717220313323

image-20210717220449148

image-20210717225908393

 $(function(){
            $('.nav>li').mouseover(function(){
                $(this).children('ul').show()
            })
            $('.nav>li').mouseout(function(){
                $(this).children('ul').hide()
            })
        })

image-20210717225750878

image-20210717230234036

image-20210717230243093

image-20210718121541941

        $(function(){
            $('#left li').mouseenter(function(){
                var index = $(this).index()
                // $('#content div').eq(index).show();
                // 4. 让他的兄弟隐藏起来
                // $('#content div').eq(index).siblings().hide()
                //   链式编程!!!!!!!!!!!!!!!!!!!
                $('#content div').eq(index).show().siblings().hide()
            })
        })

image-20210718121907124

2.jQuery样式操作

image-20210718122532741

image-20210718122726582

image-20210718144852974

image-20210718180723417

<div class="tab_list">
        <ul>
            <li class="current">商品介绍</li>
            <li>商品参数</li>
            <li>商品评价</li>
        </ul>
    </div>
    <div>
        <div class="item" style="display: block;">
            商品介绍
        </div>
        <div class="item">
            商品参数
        </div>
        <div class="item">
            商品评价
        </div>
    </div>
    <script>
        $(function(){
            $('li').click(function(){
                $(this).addClass('current').siblings().removeClass('current');
                var index = $(this).index()
                $('.item').eq(index).show().siblings().hide()
            })
        })
    </script>

image-20210718181238818

image-20210718181246571

3.jQuery效果

image-20210718182046067

image-20210718182423931

image-20210718183144618

   <div class="tab_list">
        <ul>
            <li class="current">商品介绍</li>
            <li>商品参数</li>
            <li>商品评价</li>
        </ul>
    </div>
    <div>
        <div class="item" style="display: block;">
            商品介绍
        </div>
        <div class="item">
            商品参数
        </div>
        <div class="item">
            商品评价
        </div>
    </div>
    <script>
        $(function(){
            $('li').click(function(){
                $(this).addClass('current').siblings().removeClass('current');
                var index = $(this).index()
                $('.item').eq(index).show(1000,"linear",()=>{
                    console.log('显示');
                }).siblings().hide(1000,'linear',()=>{
                    console.log('隐藏');
                })
            })
        })

image-20210718183537234

image-20210718183838607

image-20210718184742262

image-20210718184946007

  $(function(){
            $('button').hover(()=>{
                $('div').slideDown(1000,'linear')
            },()=>{
                $('div').slideUp(1000,'linear')
            })
        })
//简单写法
$(function(){
            $('button').hover(()=>{
                $('div').slideToggle(1000,'linear')
            })
        })

 //纯CSS实现!!!!!!!!不能加display属性了不过
 <style>
        div {
            height: 0;
            background-color: pink;
            transition: height 1s linear;
        }
        button:hover + div  {
            height: 100px;
        }
    </style>
</head>
<body>
    <button>asd</button>
    <div></div>

image-20210718201530062

  $(function(){
            $('button').hover(()=>{
                //stop()方法必须写在动画前
                $('div').stop().slideToggle(1000,'linear')
            })
        })

image-20210718201939113

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kZ3PSufZ-1628410202203)(C:\Users\KAN\AppData\Roaming\Typora\typora-user-images\image-20210718202134389.png)]

image-20210719202206543

image-20210719213114870

4.jQuery属性操作

image-20210719231010567

image-20210719231212566

image-20210719231539348

<body>
    <a href="https://www.baidu.com" title="都挺好">链接</a>
    <input type="checkbox" name="" id="" checked>
    <div index='1' data-index="24">我是div</div>
    <span index="1">234</span>
    <script>
        $(function(){
            // 1. element.prop("属性名") 获取元素固有的属性
            console.log($('a').prop('href'));
            $("a").prop("title","我们都挺好");
            $("input").change(function(){
                console.log($(this).prop('checked'));
            })

            // 不是元素固有属性,这里不能用prop()去获取
            // console.log($("div").prop("index")); 
            // 2.元素的自定义属性 我们通过 attr();
            console.log($('div').attr("index"));
            $('div').attr("index",4)
            console.log($('div').attr("data-index"));

            // 3.数据缓存 data() 这个里面的数据是存放在元素的内存里面
            $("span").data("uname","andy") //存在了data-uname里
            console.log($("span").data("uname")); // 获取了 data-uname 里的数据
            //这个方法获取data-index h5自定义属性 第一个 不用写 data- !!! 而且返回的是数字型
            console.log($("div").data("index"))  // 获取了 data-index 里的数据
        })
    </script>
</body>

image-20210720102639466

   <div class="check_box">
        <div>顶部全选<input type="checkbox" name="" class="check_all"></div>
        <ul>
            <li>子项<input type="checkbox" class="item_check"></li>
            <li>子项<input type="checkbox" class="item_check"></li>
            <li>子项<input type="checkbox" class="item_check"></li>
        </ul>
        <div>底部全选<input type="checkbox" class="check_all"></div>
    </div>
</body>
<script>
    // 1.全选 全不选功能模块
    // 就是把全选按钮的状态赋值给 三个小的按钮就可以了
    $(function(){
        $('.check_all').change(function(){
            var checked = $(this).prop('checked')
            // console.log(checked);
            $('.item_check, .check_all').prop('checked',checked)
        })
    })
    // 2. 如果小复选框被选中的个数等于3 就应该把全选按钮选上,否则全选按钮不选
    $(function(){
        $('.item_check').change(function(){
            //if (被选中的复选框的个数 === 3){
                // 就要选中全选按钮
            // }  else {
            //  不要选中全选按钮
            // }
            // console.log($('.item_check:checked').length);
            if ($('.item_check:checked').length === $(".item_check").length ) {
                $('.check_all').prop('checked',true);
            } else {
                $('.check_all').prop('checked',false);
            }
        })
        
    })
</script>

5.jQuery文本属性值

image-20210720104202373

image-20210720104247944

image-20210720105700211

image-20210720115126729

6.jQuery元素操作

image-20210720115110031

<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <script>
        $(function(){
            var sum = 0;
            var arr = ["red","green","blue"]
            $('div').each(function(index,domElm){
                // console.log(i);
                // 回调函数的第二个参数一定是 dom元素对象
                // console.log(domElm);
                // domElm,css('color'); dom元素对象没有css方法
                $(domElm).css('color',arr[index])
                sum += parseInt($(domElm).text())
            })
            console.log(sum);
        })
    </script>
</body>

image-20210720120916905

    <script>
        $(function(){
            var sum = 0;
            var arr = ["red","green","blue"]
            $('div').each(function(index,domElm){
                // console.log(i);
                // 回调函数的第二个参数一定是 dom元素对象
                // console.log(domElm);
                // domElm,css('color'); dom元素对象没有css方法
                $(domElm).css('color',arr[index])
                sum += parseInt($(domElm).text())
            })
            // console.log(sum);
            $.each($("div"),function(i,ele){
                console.log(i);
                console.log(ele);
            });

            // each 也能遍历对象和数组哦
            $.each(arr,function(i,e){
                console.log(i); 
                console.log(e);
            })

        })

image-20210803214658843

image-20210803214711664

image-20210803214720151

image-20210720162023631

<body>
    <ul>
        <li>
            原先
        </li>
    </ul>
    <div class="text">原来div</div>
    <script>
        
        $(function () {
            // 1. 创建元素
            var li = $("<li>后来</li>")

            // 2.添加元素
            // (1) 内部添加
            //$('ul').append(li) //内部添加并且放到内容的最后面
            $('ul').prepend(li) //内部添加并且放到内容的最前面   注意一个变量只能添加一次,不能同时添加到前或后

            // (2)外部添加
            var div = $('<div>我是后面添加的div</div>');
            $(".text").after(div)
            $(".text").before(div)

            // 3.删除元素
            $("ul").remove();  // 自己都会杀掉
            $("ul").empty();  // 可以删除匹配元素里面的子节点 孩子
            $("ul").html('')   // 同第二个效果一样
        })
    </script>
</body>

image-20210720170113455

7.jQuery尺寸、位置操作

image-20210720172311861

image-20210720174155282

image-20210720174646407

image-20210720183306213

image-20210720183619604

image-20210721171333664

image-20210721173637512

四、jQuery事件

1.JQuery事件注册

image-20210721175057005

2.JQuery事件处理

image-20210721180126604

image-20210721180300518

      $(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","red")
            //     },
            //     mouseleave:function(){
            //         $(this).css("background","purple")
            //     },
            //     click:function(){
            //         $(this).css("background","green")
            //     }
            // })

            $('div').on("mouseenter mouseleave",function(){
                $(this).toggleClass("current")
            })
        })

image-20210721181131673

image-20210721181631435

    $(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","red")
            //     },
            //     mouseleave:function(){
            //         $(this).css("background","purple")
            //     },
            //     click:function(){
            //         $(this).css("background","green")
            //     }
            // })

            // $('div').on("mouseenter mouseleave",function(){
            //     $(this).toggleClass("current")
            // })

            // (2) on可以实现事件委托(委派)
            // $("ul li").click();

            // $('ul').on('click',"li",function(e){
            //    console.log(e);
            // })

            // (3) on 可以给动态创建的元素绑定事件
            // $('ol li').click(function(){
            //     alert(11) //无效果
            // })

            $("ol").on('click',"li",function(){
                alert(1); // 有效果
            })
            var li = $('<li>我是后来创建的</li>')
            $("ol").append(li)
        })

image-20210721194353807

 $(function () {
            // 1. 添加小li
            $(".btn").on("click", function () {
                var li = $("<li></li>");
                li.html($(".txt").val() + "<a href='javascript:;'>删除</a>");
                $('ul').prepend(li)
                li.slideDown();
                $(".txt").val('')
            })
            // 2.删除小li
            // 这样不能给未来出现的li添加事件
            // $("ul li a").click(function () {
            //     alert(01)
            // })
            
            // 可以给动态创建的元素添加事件
            $('ul').on('click','a',function(){
                $(this).parent().slideUp(function(){
                    $(this).remove();
                });
            })
        })

image-20210721200342560

image-20210721201245079

image-20210721201549828

3.JQuery事件对象

image-20210721201729070

五、jQuery其他方法

1.jQuery拷贝对象

image-20210721211514998

2.多库共存

image-20210721212448016

3.jQuery插件

image-20210721220030941

image-20210721234136922

image-20210721234539938

ToDoList案例

image-20210724202304690![image-20210724231507552](https

image-20210727132519738

image-20210727133014050

image-20210727135428518

image-20210727141855734

]

3.JQuery事件对象

image-20210721201729070

五、jQuery其他方法

1.jQuery拷贝对象

image-20210721211514998

2.多库共存

image-20210721212448016

3.jQuery插件

image-20210721220030941
image-20210721234136922
image-20210721234539938

ToDoList案例

image-20210724202304690

image-20210727132519738

image-20210727133014050

image-20210727135428518
image-20210727141855734

image-20210727153029388

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值