jQuery

1、引入jQuery

1.从jQuery.com下载jquery库
2从CDN中载入jQuery

2、基本语法

基础语法:$(selector).action()

$定义jQuery
选择符(selector)“查询”和“查找”HTML元素
jQuery的action()执行对元素的操作

举个栗子

<body>
    <p>我是一个p元素</p>
    <p>我是一个p元素</p>
    <p>我是一个p元素</p>
<script>
   $(document).ready(function(){
    $("p").click(function(){
        $(this).hide();
    })
   })
</script>
</body>

3.事件

1.常用事件方法

点击事件:单击、双击
获取焦点
鼠标的各种事件

2.绑定事件
   <body>
    <button id="clickMeBtn">btn</button>

<script>
   $(document).ready(function(){
      $("#clickMeBtn").bind("click",clickHandler)
   });
   function clickHandler(e){
    console.log("clickHandler");
   }
</script>
</body>

bind可以用on代替,bind的底层实现就是用on

3.解除绑定事件
<body>
    <button id="clickMeBtn">btn</button>

<script>
   $(document).ready(function(){
      $("#clickMeBtn").bind("click",clickHandler)
      $("#clickMeBtn").unbind("click",clickHandler)
   });
   function clickHandler(e){
    console.log("clickHandler");
   }
</script>
</body>

unbind可以用off代替

4.事件的目标
5.事件的冒泡
6.自定义事件
<button id="clickMeBtn">点我</button>

<script>
   $(document).ready(function(){
        $("#clickMeBtn").click(function(){
            var e= jQuery.Event("myEvent");
            $("#clickMeBtn").trigger(e);

        });
        $("#clickMeBtn").bind("myEvent",function(event){
            console.log(event);
        })

   })
</script>
</body>

4.jQuery的HTML操作

1.jQuery捕获
<body>
    <p id="text">this is a p<a>这是一个a标签</a></p>
    <button id="#btn">btn</button>

<script>
   $(document).ready(function(){
        $("#btn").click(function(){
            alert("text"+$("#text").text());
        })
   })
</script>
</body>
<body>
    <p id="text">this is a p<a>这是一个a标签</a></p>
    <button id="#btn">btn</button>

<script>
   $(document).ready(function(){
        $("#btn").click(function(){
            alert("text"+$("#text").html());
        })
   })
</script>
</body>

html可以获取内部的子标签,text只能获取具体的内容

获取input的value值
<body>
    <p id="text">this is a p<a>这是一个a标签</a></p>
    <button id="#btn">btn</button>
     <input type="text" id="inp" value="大家好">
<script>
   $(document).ready(function(){
        $("#btn").click(function(){
            alert("text"+$("#inp").val());
        })
   })
</script>
</body>

获取属性

<body>
    <p ><a href="http://www.baidu.com" id="aid"></a></p>
    <button id="#btn">btn</button>
<script>
   $(document).ready(function(){
        $("#btn").click(function(){
            alert("text"+$("#aid").attr("href"));
        })
   })
</script>
</body>
2.jQuery设置
<body>
    <p id="p1">hello world</p>
    <button id="btn">点我1</button>
    <p id="p2">hello world</p>
    <button id="btn2">点我2</button>
<script>
   $(document).ready(function(){
        $("#btn2").click(function(){
            $("#p1").text("点我你就变了哈哈哈哈")
        });
        $("#btn2").click(function(){}
            $("#p1").html("<a>猜我变成什么样</a>")
        });
   })
</script>
</body>
3.jQuery添加元素
 function appendText(){
    html/jquery/Dom
    var text1 = '<p>iwen</p>';
    var text2 = $("<p></p>").text("ime");
    var text3 = document.creteElement("p");
    text3.innerHtml = "acely";
    $("body").append(text1,text2,text3);
   }

append在被选中元素的结尾插入内容
prepend在被选中元素的开头插入内容
before在被选中元素的之前插入内容
after在被选入元素之后插入内容

<body>
    <p id="p1">hello world</p>
    <button id="btn">btn</button>
    
<script>
   $(document).ready(function(){
        $("#btn").click(function(){
            $("#p1").append("这是我用append添加的内容");
        })
        $("#btn").click(function(){
            $("#p1").prepend("这是我用prepend添加的内容");
        })
        $("#btn").click(function(){
            $("#p1").before("这是我用before添加的内容");
        })
        $("#btn").click(function(){
            $("#p1").after("这是我用after添加的内容");
        })
   })

4.jQuery删除元素

remove全部删除
empty删除里面的子元素

<body>
    <div id="div" style="width: 200px;height: 200px;border: 1px solid black;background-color: #ededed">
        hello
        <p >hello world</p>
        <p>hello</p>
     </div>
    <button id="btn">删除</button>
<script>
   $(document).ready(function(){
    $("#btn").click(function(){
        $("#div").remove();
    });
   });
</script>
<body>
    <div id="div" style="width: 200px;height: 200px;border: 1px solid black;background-color: #ededed">
        hello
        <p >hello world</p>
        <p>hello</p>
     </div>
    <button id="btn">删除</button>
<script>
   $(document).ready(function(){
    $("#btn").click(function(){
        $("#div").empty();
    });
   });
</script>
</body>

5.jQuery的效果

1.淡入淡出

fadeIn()淡入

<body>
    <button id="in">fadein</button>
    <div id="div1" style="width: 80px;height: 80px;background-color: aqua;display: none;"></div>
    <div id="div2" style="width: 80px;height: 80px;background-color: red;display: none;"></div>
    <div id="div3" style="width: 80px;height: 80px;background-color: green;display: none;"></div>

<script>
   $(document).ready(function(){
        $("#in").on("click",function(){
            $("#div1").fadeIn(1000);
            $("#div2").fadeIn(1000);
            $("#div3").fadeIn(1000);
        })
   })
</script>
</body>

fadeOut()淡出

<body>
   <button id="out">fadeout</button>
   <div id="div1" style="width: 80px;height: 80px;background-color: aqua;"></div>
   <div id="div2" style="width: 80px;height: 80px;background-color: red;"></div>
   <div id="div3" style="width: 80px;height: 80px;background-color: green;"></div>

<script>
  $(document).ready(function(){
       $("#out").on("click",function(){
           $("#div1").fadeOut(1000);
           $("#div2").fadeOut(1000);
           $("#div3").fadeOut(1000);
       })
  })
</script>
</body>

fadeTo()淡出的时候设置透明度

<body>
    <button id="in">fadein</button>
    <button id="out">fadeout</button>
    <button id="toggle">toggle</button>
    <button id="to">to</button>
    <div id="div1" style="width: 80px;height: 80px;background-color: aqua;display: none;"></div>
    <div id="div2" style="width: 80px;height: 80px;background-color: red;display: none;"></div>
    <div id="div3" style="width: 80px;height: 80px;background-color: green;display: none;"></div>

<script>
   $(document).ready(function(){
        $("#in").on("click",function(){
            $("#div1").fadeIn(1000);
            $("#div2").fadeIn(1000);
            $("#div3").fadeIn(1000);
        });
        $("#out").on("click",function(){
            $("#div1").fadeOut(1000);
            $("#div2").fadeOut(1000);
            $("#div3").fadeOut(1000);
        });
        $("#toggle").on("click",function(){
            $("#div1").fadeToggle(1000);
            $("#div2").fadeToggle(1000);
            $("#div3").fadeToggle(1000);
        });
        $("#to").on("click",function(){
            $("#div1").fadeTo(1000,0.5);
            $("#div2").fadeTo(1000,0.7);
            $("#div3").fadeTo(1000,0.3);
        })
   })
</script>
</body>

效果对比图


图片.png
图片.png

fadeToggle()相当于开关,既可淡入也可淡出

2.滑动

slideDown()滑出
slideToggle()既可以滑出也可以隐藏
slideUp()隐藏

<style>
    #content,#flipshow,#fliphide,#fliptoggle{
        padding:5px;
        text-align: center;
        background-color: #ece023;
        border: solid 1px #ece099;
    }
   #content{
    padding:50px;
    display: none;
   }  
    </style>

</head>
<body>
    <div id="flipshow">出现</div>
    <div id="fliphide">隐藏</div>
    <div id="fliptoggle">同时操作</div>
    <div id="content">hello world</div>

<script>
   $(document).ready(function(){
     $("#flipshow").click(function(){
        $("#content").slideDown(1000);
     });
     $("#fliphide").click(function(){
        $("#content").slideUp(1000);
     });
     $("#fliptoggle").click(function(){
        $("#content").slideToggle(1000);
     });
   })
</script>
</body>
3.回调
<body>
    <button>隐藏</button>
    <p>hello world hello world hello world hello world</p>

<script>
   $(document).ready(function(){
        $("button").click(function(){
            $("p").hide(1000,function(){
                alert("动画结束,这个方法被回调")
            })
            
        })
   })
</script>
4.隐藏与显示

toggle既可以控制显示也可以控制隐藏

<body>
    <p>hello</p>
    <button id="hide">隐藏</button>
    <button id="show">显示</button>
    <button id="toggle">可控制</button>
<script>
   $(document).ready(function(){
        $("#hide").click(function(){
                $("p").hide(1000);

        });
        $("#show").click(function(){
                $("p").show(1000);

        });
        $("#toggle").click(function(){
                $("p").toggle(1000);

        })
   })
</script>
</body>

6.jQuery的扩展

1.扩展
2.no Conflict

消除掉$对于jQuery的缩写
对于jQuery比较难拼写,可声明一个对象代替

<body>
    <div>hello</div>
    <button id="btn">btn</button>
<script>
   var myjq = $.noConflict();
   myjq(document).ready(function(){
        myjq("#btn").on("click",function(){
            myjq("div").text("new hello");
        })
   })
</script>
</body>

7.CSS操作与盒子模型

1.css操作
<body>
    <div></div>

<script>
   $(document).ready(function(){
    $("div").css("width","100px");
    $("div").css("height","100px");
    $("div").css("background","red");
   })
</script>
</body>

另一种方式

<body>
    <div></div>

<script>
   $(document).ready(function(){
      $("div").css({
       width:"100px",
       height:"100px",
       background:"red"
      })
   })
</script>
</body>

可以通过addClass("类名")来设置css样式
removeClass("类名")移出掉之前的style样式

2.盒子模型

height() width()元素本身的高度,不包括margin boder padding
innerheight() innerwidth()包括元素本身加margin padding
outerHeight(true)整个盒子的全部高度
outerHeight()元素本身,内边距padding,边框boder

8.元素

1.元素的遍历

向上遍历:子级找到父级一直向上
parent()最近的一个父元素
parents()是指所有的父元素
parentUntil()指的是由p元素到div1这个区间

  $("p").parentUntil("#div1").css({border:"3px solid red"})

向下遍历
children()可选参数只向下遍历一级针对它的儿子辈
find()必选参数,可以针对它的所有子元素

<style>
        #div1{
            width: 500px;
            height: 200px;
            border: 3px solid chocolate;
        }
        #div2{
            width: 400px;
            height: 150px;
            margin-top: 10px;
            margin-left: 10px;
            border: 3px solid chocolate;
        }
        p{
            margin-top: 10px;
            margin-left: 10px;
            width: 150px;
            height: 80px;
            border: 3px solid chocolate;
        }
    </style>
</head>
<body>
    <div id="div1">div1
        <div id="div2">div2
            <p>
                <a>
                    hello
                </a>
            </p>
        </div>
    </div>

<script>
   $(document).ready(function(){
       $("#div1").children("#div2").css({border:"3px solid red"})
        $("#div1").find("a").css({border:"3px solid red"})
   })
</script>
</body>

同级遍历
sibings()除了h4其他同级的元素都被修改掉

$(document).ready(function(){
        $("h4").siblings().css({
            border:"3px solid red"
        })
   })

next()h4的下一个元素被修改

 $(document).ready(function(){
        $("h4").next().css({
            border:"3px solid red"
        })
   })

nextAll()下面所有的元素都被修改
nextUntil()h4到h6的区间(向下)

 $(document).ready(function(){
        $("h4").nextUntil("h6").css({
            border:"3px solid red"
        })
   })

prev()上一个元素被修改
prevAll()向上的所有元素被修改
prevUntil()区间

2.元素的过滤

first()寻找同样元素的第一个
last()最后一个
eq(id)返回当前元素的索引id
filter(“标准”)满足这个集合的所有元素
net(“标准”)不满足这个集合的所有元素

9.菜单

1.垂直菜单实现
2.水平菜单实现
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,063评论 6 510
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,805评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,403评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,110评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,130评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,877评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,533评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,429评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,947评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,078评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,204评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,894评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,546评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,086评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,195评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,519评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,198评论 2 357

推荐阅读更多精彩内容

  • 为甚嚒要学习jQuery? 因为JS中有很多痛点: window.onload事件只能出现一次,如果出现多次,后面...
    magic_pill阅读 826评论 0 13
  • 一:认识jquery jquery是javascript的类库,具有轻量级,完善的文档,丰富的插件支持,完善的Aj...
    xuguibin阅读 1,713评论 1 7
  • jQuery模块 选择器、DOM操作、事件、AJAX与动画 匿名函数自执行 作用:解决命名空间与变量污染的问题 总...
    青青玉立阅读 884评论 0 0
  • jQuery笔记总结篇 poetries 已关注 2016.10.20 10:52* 字数 9137 阅读 660...
    小杰的简书阅读 1,798评论 2 32
  • 目前店群裂变,在我看来,技术壁垒稍微高一点 但是可复制性降低 单产变高,成本随之变高 一个月做10万纯利即可 放大...
    写个锤子阅读 176评论 0 0