jquery(一)

來源:互聯網
上載者:User

標籤:操作   lang   until   doctype   .class   style   優勢   body   tor   

一 jQuery是什嗎? 

[1]   jQuery由美國人John Resig建立,至今已吸引了來自世界各地的眾多 javascript高手加入其team。

[2]   jQuery是繼prototype之後又一個優秀的Javascript架構。其宗旨是——WRITE LESS,DO MORE!

[3]  它是輕量級的js庫(壓縮後只有21k) ,這是其它的js庫所不及的,它相容CSS3,還相容各種瀏覽器

[4]  jQuery是一個快速的,簡潔的javaScript庫,使使用者能更方便地處理HTMLdocuments、events、實現動畫效果,並且方便地為網站提供AJAX互動。

[5]  jQuery還有一個比較大的優勢是,它的文檔說明很全,而且各種應用也說得很詳細,同時還有許多成熟的外掛程式可供選擇。

 二 什麼是jQuery對象?

jQuery 對象就是通過jQuery封裝DOM對象後產生的對象。jQuery 對象是 jQuery 專屬的如果一個對象是 jQuery 對象那麼它就可以使用 jQuery 裡的方法: $(“#test”).html();

$("#test").html() 
意思是指:擷取ID為test的元素內的html代碼。其中html()是jQuery裡的方法 這段代碼等同於用DOM實現代碼: document.getElementById(" test ").innerHTML; 雖然jQuery對象是封裝DOM對象後產生的,但是jQuery無法使用DOM對象的任何方法,同理DOM對象也不能使用jQuery裡的方法.亂使用會報錯 約定:如果擷取的是 jQuery 對象, 那麼要在變數前面加上$. var $variable = jQuery 對象var variable = DOM 對象$variable[0]:jquery對象轉為dom對象 $("#msg").html(); $("#msg")[0].innerHTML

 jquery的基礎文法:$(selector).action()     

 參考:http://jquery.cuishifeng.cn/

 三 尋找元素(選取器和篩選器) 3.1   選取器3.1.1 基本選取器      ?
1 $("*")  $("#id")   $(".class")  $("element")  $(".class,p,div")
3.1.2 層級選取器   ?
1 $(".outer div")  $(".outer>div")   $(".outer+div")  $(".outer~div")
3.1.3 基本篩選器  ?
1 $("li:first")  $("li:eq(2)")  $("li:even") $("li:gt(1)")
3.1.4 屬性選取器?
1 $(‘[id="div1"]‘)   $(‘["alex="sb"][id]‘)
3.1.5 表單選取器      ?
1 $("[type=‘text‘]")----->$(":text")         注意只適用於input標籤  : $("input:checked")
3.1.6 表單屬性選取器
    :enabled    :disabled    :checked    :selected
<body><form>    <input type="checkbox" value="123" checked>    <input type="checkbox" value="456" checked>  <select>      <option value="1">Flowers</option>      <option value="2" selected="selected">Gardens</option>      <option value="3" selected="selected">Trees</option>      <option value="3" selected="selected">Trees</option>  </select></form><script src="jquery.min.js"></script><script>    // console.log($("input:checked").length);     // 2    // console.log($("option:selected").length);   // 只能預設選中一個,所以只能lenth:1    $("input:checked").each(function(){        console.log($(this).val())    })</script></body>
View Code3.2 篩選器3.2.1  過濾篩選器    ?
1 $("li").eq(2)  $("li").first()  $("ul li").hasclass("test")
3.2.2  尋找篩選器  
 尋找子標籤:         $("div").children(".test")      $("div").find(".test")                                  向下尋找兄弟標籤:    $(".test").next()               $(".test").nextAll()     
$(".test").nextUntil() 向上尋找兄弟標籤: $("div").prev() $("div").prevAll()
$("div").prevUntil() 尋找所有兄弟標籤: $("div").siblings()
尋找父標籤: $(".test").parent() $(".test").parents()
$(".test").parentUntil()
 四 操作元素(屬性,css,文檔處理)4.1 事件頁面載入?
12 ready(fn)  // 當DOM載入就緒可以查詢及操縱時綁定一個要執行的函數。$(document).ready(function(){}) -----------> $(function(){})  
事件綁定
//文法:  標籤對象.事件(函數)    eg: $("p").click(function(){})
事件委派:
$("").on(eve,[selector],[data],fn)  // 在選擇元素上綁定一個或多個事件的事件處理函數。
<ul>    <li>1</li>    <li>2</li>    <li>3</li></ul><hr><button id="add_li">Add_li</button><button id="off">off</button><script src="jquery.min.js"></script><script>    $("ul li").click(function(){        alert(123)    });    $("#add_li").click(function(){        var $ele=$("<li>");        $ele.text(Math.round(Math.random()*10));        $("ul").append($ele)    });//    $("ul").on("click","li",function(){//        alert(456)//    })     $("#off").click(function(){         $("ul li").off()     })    </script>
事件切換

hover事件:

一個模仿懸停事件(滑鼠移動到一個對象上面及移出這個對象)的方法。這是一個自訂的方法,它為頻繁使用的任務提供了一種“保持在其中”的狀態。

over:滑鼠移到元素上要觸發的函數

out:滑鼠移出元素要觸發的函數

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <style>        *{            margin: 0;            padding: 0;        }        .test{            width: 200px;            height: 200px;            background-color: wheat;        }    </style></head><body><div class="test"></div></body><script src="jquery.min.js"></script><script>//    function enter(){//        console.log("enter")//    }//    function out(){//        console.log("out")//    }// $(".test").hover(enter,out)$(".test").mouseenter(function(){        console.log("enter")});$(".test").mouseleave(function(){        console.log("leave")    });</script></html>
View Code4.2 屬性操作
--------------------------CSS類$("").addClass(class|fn)$("").removeClass([class|fn])
--------------------------屬性$("").attr();$("").removeAttr();$("").prop();$("").removeProp();
--------------------------HTML代碼/文本/值$("").html([val|fn])$("").text([val|fn])$("").val([val|fn|arr])
---------------------------$("#c1").css({"color":"red","fontSize":"35px"})

attr方法使用:

<input id="chk1" type="checkbox" />是否可見<input id="chk2" type="checkbox" checked="checked" />是否可見<script>//對於HTML元素本身就帶有的固有屬性,在處理時,使用prop方法。//對於HTML元素我們自己自訂的DOM屬性,在處理時,使用attr方法。//像checkbox,radio和select這樣的元素,選中屬性對應“checked”和“selected”,這些也屬於固有屬性,因此//需要使用prop方法去操作才能獲得正確的結果。//    $("#chk1").attr("checked")//    undefined//    $("#chk1").prop("checked")//    false//  ---------手動選中的時候attr()獲得到沒有意義的undefined-----------//    $("#chk1").attr("checked")//    undefined//    $("#chk1").prop("checked")//    true    console.log($("#chk1").prop("checked"));//false    console.log($("#chk2").prop("checked"));//true    console.log($("#chk1").attr("checked"));//undefined    console.log($("#chk2").attr("checked"));//checked</script>
View Code

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.