【2017-06-02】Jquery基礎

來源:互聯網
上載者:User

標籤:屬性   ...   turn   單元   blog   stop   回呼函數   code   position   

 

Jquery就是Js整合的一些方法包。

注意:Jquery的引入位置在<head></head>裡。

一、選取器

1、Id選取器

    $("#div1")

2、Class選取器

    $(".div")

    用class選取器選出一堆對象給他們附事件,不用像Js一樣遍曆。直接附加事件,取到的所有對象都會附上事件。

    注意:Js中的this,在Jquery中變為$(this)。

3、標籤選取器

    $("div")

4、並欄選取器

    用,隔開

    $("#div1,#div2")

5、後代選取器 

    用空格隔開

6、過濾選取器

    第一個:$(".div:first") 

    最後一個:$(".div:last") 

    任意個:$(".div:eq(索引號)") 

               $(".div").eq(索引號) 

    大於:$(".div:gt(索引號)")     取到的是索引號以後的 

    小於:$(".div:lt(索引號)")     取到的是索引號之前的 

    排除:$(".div:not(選取器)")     取到的是排除掉選擇的剩下的 

            $(".div:not(.div:eq(2))")  取到的是排除掉第三個剩下的

    奇數個:$(".div:odd")   索引奇數個

    偶數個:$(".div:even")  索引偶數個,包含0

    屬性名稱過濾:$(".div[屬性名稱]")    

    屬性名稱+值過濾:$(".div[屬性名稱=值]")    $(".div[屬性名稱!=值]")

    內容過濾:$(".div:contains(‘字串‘)") 

    包含子項目:$(".div:has(‘選取器‘)") 

 

二、事件:

1、基本事件:

就是把js事件中的on去掉。

2、複合事件

hover(function(){},function(){})   

就是把滑鼠移入移出事件合在一起了,第一個function裡是移入事件,第二個function是移出事件。

toggle(function(){},function(){},....)     需要引Jquery1.7.1版本。

滑鼠點擊迴圈執行裡面的方法。可以放無數個function

3、未來元素附事件

對象.live("事件名",function(){});

給還沒有建立出來的對象附上事件。

 

4、阻止事件冒泡

在事件function裡最後return false;

 

 

三、Dom操作

(一)操作屬性

1、擷取屬性

var s = $("選取器").attr("屬性名稱")

2、設定屬性

$("選取器").attr("屬性名稱","屬性值")

3、刪除屬性

$("選取器").removeAttr("屬性名稱")

 

(二)操作樣式

 1、操作內聯樣式

擷取樣式

var s = $("選取器").css("樣式名")

設定樣式

$("選取器").css("樣式名","值")

 

 2、操作樣式表的class

添加class,相當於並列class。

$("選取器").addClass("class名")

移除class

$("選取器").removeClass("class名")

添加移除交替class

$("選取器").toggleClass("class名")

 

(三)操作內容

1、表單元素

取值

var s = $("選取器").val()

賦值

$("選取器").val("值")

 

2、非表單元素

取值

var s = $("選取器").html()        var s = $("選取器").text()

賦值

$("選取器").html("內容")            $("選取器").text("內容")

 

(四)操作相關元素

1、尋找

找父級    parent()  

 

找前輩    parents(選取器)

找子集    children(選取器)

找後代    find(選取器)

找哥哥    prev()          prevAll(選取器)

找弟弟    next()          nextAll(選取器)

 

 

2、操作

建立:   $("HTML字串")

例: $("#div1").append($("<div class=‘div2‘></div>"));

添加:   appen(jquery對象)    ---往內部添加

           after(,..)                 ----下部平級添加

           before("...")            -----上部平級添加

移除:    empty()    ----清空內部全部元素

            remove()   -----移除元素

複製:   clone()

 

 

Js建立對象

var d1=document.createElement(‘div‘);

d1.setAttribute("class","div2");    //設定class屬性

$("#div1").append(d1);     //往div1下面增添。

 

 Jquery建立對象

$("#div1").append($("<div class=‘div2‘></div>"));

 

 

 

 

四、動畫

show(),hide()    顯示、隱藏。

slideDown(),slideUp()   下拉顯示,上拉隱藏

 

fadeIn(),fadeOut()  淡入淡出

 

自訂動畫

animate({left:"300px",top:"300px"},3000,function(){回呼函數})

停止動畫,防止動畫積累: .stop()

導覽列製作

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <script src="js/jquery-1.7.2.min.js"></script>    <title></title>    <style type="text/css">        .div1 {            position: relative;            width: 100px;            height: 50px;            background-color: red;            float: left;            margin-left: 20px;        }        .div2 {            position: absolute;            width: 100%;            height: 0px;            top: 50px;            background-color: green;        }    </style></head><body>    <div class="div1">        <div class="div2"></div>    </div>    <div class="div1">        <div class="div2"></div>    </div>    <div class="div1">        <div class="div2"></div>    </div>    <div class="div1">        <div class="div2"></div>    </div>    <div class="div1">        <div class="div2"></div>    </div></body></html><script type="text/javascript">    $(".div1").mouseover(function () {        var aaa = $(this).children(".div2:eq(0)");        aaa.stop().animate({ height: "300px" }, 500, function () {            aaa.css("background-color", "blue");        });    });    $(".div1").mouseout(function () {        var aaa = $(this).children(".div2:eq(0)");        aaa.stop().animate({ height: "0px" }, 500, function () {            aaa.css("background-color", "green");        });    });</script>

 

動畫色彩坡形,需要另引Jquery.color。放在之前引用的Jquery下面。

 

 

 

【2017-06-02】Jquery基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.