posts - 66, comments - 12, trackbacks - 0, articles - 0

          jquery 學習(轉帖)

          Posted on 2008-06-30 09:13 cyantide 閱讀(299) 評論(0)  編輯  收藏

          1:核心部分
          2:DOM操作
          3:css操作
          4:javascript處理
          5:動態(tài)效果
          6:event事件
          7:ajax支持
          8:插件程序


          一:核心部分

          $(expr)
          說明:該函數(shù)可以通過css選擇器,Xpath或html代碼來匹配目標元素,所有的jQuery操作都以此為基礎
          參數(shù):expr:字符串,一個查詢表達式或一段html字符串

          例子:
          未執(zhí)行jQuery前:

          <p>one</p>
          <div>
                <p>two</p>
          </div>
              <p>three</p>
              <a href="#" id="test" onClick="jq()" >jQuery</a>
          jQuery代碼及功能: function jq(){ 
              alert($("div > p").html()); 
          }運行:當點擊id為test的元素時,彈出對話框文字為two,即div標簽下p元素的內(nèi)容 function jq(){
              $("<div><p>Hello</p></div>").appendTo("body");
          }運行:當點擊id為test的元素時,向body中添加“<div><p>Hello</p></div>”

          $(elem)
          說明:限制jQuery作用于一個特定的dom元素,這個函數(shù)也接受xml文檔和windows對象
          參數(shù): elem:通過jQuery對象壓縮的DOM元素
          例子:
          未執(zhí)行jQuery前: <p>one</p>
            <div>
               <p>two</p>
            </div><p>three</p>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
              alert($(document).find("div > p").html());
          }運行:當點擊id為test的元素時,彈出對話框文字為two,即div標簽下p元素的內(nèi)容 function jq(){
             $(document.body).background("black");
          }運行:當點擊id為test的元素時,背景色變成黑色
          $(elems)
          說明:限制jQuery作用于一組特定的DOM元素
          參數(shù): elem:一組通過jQuery對象壓縮的DOM元素
          例子:
          未執(zhí)行jQuery前: <form id="form1">
                <input type="text" name="textfield">
                <input type="submit" name="Submit" value="提交">
          </form>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
             $(form1.elements ).hide();
          }運行:當點擊id為test的元素時,隱藏form1表單中的所有元素。
          $(fn)
          說明:$(document).ready()的一個速記方式,當文檔全部載入時執(zhí)行函數(shù)。可以有多個$(fn)當文檔載入時,同時執(zhí)行所有函數(shù)!
          參數(shù):fn (Function):當文檔載入時執(zhí)行的函數(shù)!
          例子: $( function(){
              $(document.body).background("black");
          })運行:當文檔載入時背景變成黑色,相當于onLoad。

           

          $(obj)
          說明:復制一個jQuery對象,
          參數(shù):obj (jQuery): 要復制的jQuery對象

          例子:
          未執(zhí)行jQuery前: <p>one</p>
          <div>
             <p>two</p>
          </div>
          <p>three</p>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
              var f = $("div");
              alert($(f).find("p").html())
          }運行:當點擊id為test的元素時,彈出對話框文字為two,即div標簽下p元素的內(nèi)容。

           

          each(fn)
          說明:將函數(shù)作用于所有匹配的對象上
          參數(shù):fn (Function): 需要執(zhí)行的函數(shù)

          例子:
          未執(zhí)行jQuery前: <img src="1.jpg"/>
          <img src="1.jpg"/>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
             $("img").each(function(){
                  this.src = "2.jpg"; });
          }運行:當點擊id為test的元素時,img標簽的src都變成了2.jpg。
          eq(pos)
          說明:減少匹配對象到一個單獨得dom元素
          參數(shù):pos (Number): 期望限制的索引,從0 開始
          例子:
          未執(zhí)行jQuery前: <p>This is just a test.</p>
          <p>So is this</p>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
              alert($("p").eq(1).html())
          }運行:當點擊id為test的元素時,alert對話框顯示:So is this,即第二個<p>標簽的內(nèi)容
          get() get(num)
          說明:獲取匹配元素,get(num)返回匹配元素中的某一個元素
          參數(shù):get (Number): 期望限制的索引,從0 開始
          例子:
          未執(zhí)行jQuery前: <p>This is just a test.</p>
          <p>So is this</p>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
              alert($("p").get(1).innerHTML);
          }運行:當點擊id為test的元素時,alert對話框顯示:So is this,即第二個<p>標簽的內(nèi)容
          注意get和eq的區(qū)別,eq返回的是jQuery對象,get返回的是所匹配的dom對象,所有取$("p").eq(1)對象的內(nèi)容用jQuery方法html(),而取$("p").get(1)的內(nèi)容用innerHTML
          index(obj)
          說明:返回對象索引
          參數(shù):obj (Object): 要查找的對象
          例子:
          未執(zhí)行jQuery前: <div id="test1"></div>
          <div id="test2"></div>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
              alert($("div").index(document.getElementById('test1')));
              alert($("div").index(document.getElementById('test2')));
          }運行:當點擊id為test的元素時,兩次彈出alert對話框分別顯示0,1
          size()   Length
          說明:當前匹配對象的數(shù)量,兩者等價
          例子:
          未執(zhí)行jQuery前: <img src="test1.jpg"/>
          <img src="test2.jpg"/>
          <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
              alert($("img").length);
          }運行:當點擊id為test的元素時,彈出alert對話框顯示2,表示找到兩個匹配對象

          二:DOM操作

          屬性
          我們以<img id="a" scr="5.jpg"/>為例,在原始的javascript里面可以用var o=document.getElementById('a')取的id為a的節(jié)點對象,在用o.src來取得或修改該節(jié)點的scr屬性,在jQuery 里$("#a")將得到jQuery對象[ <img id="a" scr="5.jpg"/> ],然后可以用jQuery提供的很多方法來進行操作,如$("#a").scr()將得到5.jpg,$("#a").scr("1.jpg")將該對象src屬性改為1,jpg。

          下面我們來講jQuery提供的眾多jQuery方法,方便大家快速對DOM對象進行操作

          herf()   herf(val)
          說明:對jQuery對象屬性herf的操作。
          例子:
          未執(zhí)行jQuery前 <a href="1.htm" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
             alert($("#test").href());
             $("#test").href("2.html");
          }運行:先彈出對話框顯示id為test的連接url,在將其url改為2.html,當彈出對話框后會看到轉向到2.html
          同理,jQuery還提供類似的其他方法,大家可以分別試驗一下:
          herf()  herf(val)   html()  html(val)   id()  id (val)  name()  name (val)   rel()  rel (val)
          src()    src (val)   title()  title (val)   val()  val(val)

           

          操作
          after(html)  在匹配元素后插入一段html <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){ 
                 $("#test").after("<b>Hello</b>"); 
          }執(zhí)行后相當于: <a href="#" id="test" onClick="jq()">jQuery</a><b>Hello</b>
          after(elem)  after(elems)  將指定對象elem或對象組elems插入到在匹配元素后 <p id="test">after</p><a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能 function jq(){ 
               $("a").after($("#test")); 
          }執(zhí)行后相當于 <a href="#" onClick="jq()">jQuery</a><p id="test">after</p>
          append(html)在匹配元素內(nèi)部,且末尾插入指定html <a href="#" id="test" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
               $("#test").append("<b>Hello</b>"); 
          }執(zhí)行后相當于 <a href="#" onClick="jq()">jQuery<b>Hello</b></a>
          同理還有append(elem)  append(elems) before(html) before(elem) before(elems)請執(zhí)行參照append和after的方來測試、理解!

          appendTo(expr)  與append(elem)相反 <p id="test">after</p><a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能 function jq(){ 
                $("a"). appendTo ($("#test")); 
          }執(zhí)行后相當于 <p id="test">after<a href="#" onClick="jq()">jQuery</a> </p>
          clone() 復制一個jQuery對象 <p id="test">after</p><a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){ 
               $("#test").clone().appendTo($("a")); 
          }復制$("#test")然后插入到<a>后,執(zhí)行后相當于 <p id="test">after</p><a href="#" onClick="jq()">jQuery</a><p id="test">after</p>
          empty() 刪除匹配對象的所有子節(jié)點 <div id="test">
            <span>span</span>
            <p>after</p>
          </div>
          <a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){ 
              $("#test").empty(); 
          }執(zhí)行后相當于 <div id="test"></div><a href="#" onClick="jq()">jQuery</a>
          insertAfter(expr)   insertBefore(expr)
               按照官方的解釋和我的幾個簡單測試insertAfter(expr)相當于before(elem),insertBefore(expr)相當于after (elem)

          prepend (html)  prepend (elem)  prepend (elems)   在匹配元素的內(nèi)部且開始出插入
          通過下面例子區(qū)分append(elem)  appendTo(expr)  prepend (elem) <p id="a">p</p>
          <div>div</div>執(zhí)行$ ("#a").append($("div")) 后相當于 <p id="a">
            P
            <div>div</div>
          </p>執(zhí)行 $("#a").appendTo($("div"))  后 相當于 <div>
             div
             <p id="a">p</p>
          </div>執(zhí)行 $("#a").prepend ($("div"))  后 相當于 <p id="a">
             <div>div</div>
             P
          </p>
          remove()  刪除匹配對象
          注意區(qū)分empty(),empty()移出匹配對象的子節(jié)點,remove(),移出匹配對象

          wrap(htm) 將匹配對象包含在給出的html代碼內(nèi) <p>Test Paragraph.</p> <a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){ 
                $("p").wrap("<div class='wrap'></div>");
          }執(zhí)行后相當于 <div class='wrap'><p>Test Paragraph.</p></div>
          wrap(elem) 將匹配對象包含在給出的對象內(nèi) <p>Test Paragraph.</p><div id="content"></div>
          <a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){ 
                $("p").wrap( document.getElementById('content') );
          }執(zhí)行后相當于 <div id="content"><p>Test Paragraph.</p></div>
          遍歷、組合
          add(expr)  在原對象的基礎上在附加符合指定表達式的jquery對象 <p>Hello</p><p><span>Hello Again</span></p>
          <a href="#" onClick="jq()">jQuery</a>jQuery代碼及功能: function jq(){
               var f=$("p").add("span");   
               for(var i=0;i < $(f).size();i++){
               alert($(f).eq(i).html());}
          }執(zhí)行$("p")得到匹配<p>的對象,有兩個,add("span")是在("p")的基礎上加上匹配<span >的對象,所有一共有3個,從上面的函數(shù)運行結果可以看到$("p").add("span")是3個對象的集合,分別是[<p> Hello</p>],[<p><span>Hello Again</span></p>],[<span>Hello Again</span>]。

          add(el)  在匹配對象的基礎上在附加指定的dom元素。
                  $("p").add(document.getElementById("a"));

          add(els)  在匹配對象的基礎上在附加指定的一組對象,els是一個數(shù)組。
          <p>Hello</p><p><span>Hello Again</span></p>jQuery代碼及功能: function jq(){
               var f=$("p").add([document.getElementById("a"), document.getElementById("b")])
               for(var i=0;i < $(f).size();i++){
                       alert($(f).eq(i).html());}
          }注意els是一個數(shù)組,這里的[ ]不能漏掉。

          ancestors ()  一依次以匹配結點的父節(jié)點的內(nèi)容為對象,根節(jié)點除外(有點不好理解,看看下面例子就明白了) <div>
              <p>one</p>
              <span>
              <u>two</u>
              </span>
          </div>jQuery代碼及功能: function jq(){
               var f= $("u").ancestors();
               for(var i=0;i < $(f).size();i++){
                alert($(f).eq(i).html());}
          }第一個對象是以<u>的父節(jié)點的內(nèi)容為對象,[ <u>two</u> ]
          第一個對象是以<u>的父節(jié)點的父節(jié)點(div)的內(nèi)容為對象,[<p>one</p><span><u>two</u></span> ]
          一般一個文檔還有<body>和<html>,依次類推下去。

          ancestors (expr)  在ancestors()的基礎上之取符合表達式的對象
          如上各例子講var f改為var f= $("u").ancestors(“div”),則只返回一個對象:
          [ <p>one</p><span><u>two</u></span>  ]

          children()  返回匹配對象的子介點 <p>one</p>
          <div id="ch">  
               <span>two</span>
          </div>jQuery代碼及功能: function jq(){
              alert($("#ch").children().html());
          }$("#ch").children()得到對象[ <span>two</span> ].所以.html()的結果是”two”

          children(expr)  返回匹配對象的子介點中符合表達式的節(jié)點 <div id="ch">  
                <span>two</span>
                <span id="sp">three</span>
          </div>jQuery代碼及功能 function jq(){
              alert($("#ch").children(“#sp”).html());
          }$("#ch").children()得到對象[<span>two</span><span id="sp">three</span> ].
          $("#ch").children(“#sp”)過濾得到[<span id="sp">three</span> ]

          parent ()  parent (expr)取匹配對象父節(jié)點的。參照children幫助理解

          contains(str)  返回匹配對象中包含字符串str的對象 <p>This is just a test.</p><p>So is this</p>jQuery代碼及功能: function jq(){
              alert($("p").contains("test").html());
          }$("p")得到兩個對象,而包含字符串”test”只有一個。所有$("p").contains("test")返回 [ <p>This is just a test.</p> ]

          end() 結束操作,返回到匹配元素清單上操作前的狀態(tài).

          filter(expr)   filter(exprs)   過濾現(xiàn)實匹配符合表達式的對象 exprs為數(shù)組,注意添加“[ ]” <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>jQuery代碼及功能: function jq(){
              alert($("p").filter(".selected").html())
          }$("p")得到三個對象,$("p").contains("test")只返回class為selected的對象。

          find(expr)  在匹配的對象中繼續(xù)查找符合表達式的對象 <p>Hello</p><p id="a">Hello Again</p><p class="selected">And Again</p>Query代碼及功能: function jq(){
              alert($("p").find("#a").html())
          }在$("p")對象中查找id為a的對象。

          is(expr)  判斷對象是否符合表達式,返回boolen值 <p>Hello</p><p id="a">Hello Again</p><p class="selected">And Again</p>Query代碼及功能: function jq(){
              alert($("#a").is("p"));
          }在$("#a ")是否符合jquery表達式。
          大家可以用$("#a").is("div");  ("#a").is("#a")多來測試一下

          next()  next(expr)  返回匹配對象剩余的兄弟節(jié)點 <p>Hello</p><p id="a">Hello Again</p><p class="selected">And Again</p>jQuery代碼及功能 function jq(){
                  alert($("p").next().html());
                  alert($("p").next(".selected").html());
          }$("p").next()返回 [ <p id="a">Hello Again</p> , <p class="selected">And Again</p> ]兩個對象
          $("p").next(".selected)只返回 [<p class="selected">And Again</p> ]一個對象

          prev ()  prev (expr)  參照next理解

          not(el)  not(expr)  從jQuery對象中移出匹配的對象,el為dom元素,expr為jQuery表達式。 <p>one</p><p id="a">two</p>
          <a href="#" onclick="js()">jQuery</a>jQuery代碼及功能: function js(){
               alert($("p").not(document.getElementById("a")).html());
               alert($("p").not(“#a”).html());
          }$("p")由兩個對象,排除后的對象為[<p>one</p> ]

          siblings ()  siblings (expr)  jquery匹配對象中其它兄弟級別的對象 <p>one</p>
          <div>
            <p id="a">two</p>
          </div>
          <a href="#" onclick="js()">jQuery</a>jQuery代碼及功能: function js(){
                 alert($("div").siblings().eq(1).html());
          }$("div").siblings()的結果實返回兩個對象[<p>one</p>,<a href="#" onclick="js()">jQuery</a> ]
          alert($("div").siblings(“a”)返回一個對象[<a href="#" onclick="js()">jQuery</a> ]

          其他
          addClass(class)   為匹配對象添加一個class樣式
          removeClass (class)   將第一個匹配對象的某個class樣式移出

          attr (name)   獲取第一個匹配對象的屬性 <img src="test.jpg"/><a href="#" onclick="js()">jQuery</a> jQuery代碼及功能: function js(){
               alert($("img").attr("src"));
          }返回test.jpg

          attr (prop)   為第一個匹配對象的設置屬性,prop為hash對象,用于為某對象批量添加眾多屬性 <img/><a href="#" onclick="js()">jQuery</a>jQuery代碼及功能: function js(){
               $("img").attr({ src: "test.jpg", alt: "Test Image" });
          }運行結果相當于<img src="test.jpg" alt="Test Image"/>

          attr (key,value)   為第一個匹配對象的設置屬性,key為屬性名,value為屬性值 <img/><a href="#" onclick="js()">jQuery</a>jQuery代碼及功能 function js(){
               $("img").attr(“src”,”test.jpg”);
          }運行結果相當于<img src="test.jpg"/>

          removeAttr (name)   將第一個匹配對象的某個屬性移出 <img alt="test"/><a href="#" onclick="js()">jQuery</a>jQuery代碼及功能: function js(){
               $("img"). removeAttr("alt");
          }運行結果相當于<img />

          toggleClass (class)   將當前對象添加一個樣式,不是當前對象則移出此樣式,返回的是處理后的對象 <p>Hello</p><p class="selected">Hello Again</p><a href="#" onclick="js()">jQuery</a>$("p")的結果是返回對象 [<p>Hello</p>,<p class="selected">Hello Again</p> ]
          $("p").toggleClass("selected")的結果是實返回對象 [ <p class="selected">Hello</p>, <p>Hello Again</p> ]

          三 :CSS操作

                傳統(tǒng)javascript對css的操作相當繁瑣,比如<div id="a" style="background:blue">css</div>取它的background語法是 document.getElementById("a").style.background,而jQuery對css更方便的操作,$("#a"). background(),$("#a").background(“red”)
          $("#a")得到jQuery對象[ <div id="a" … /div> ]
          $("#a").background()將取出該對象的background樣式。
          $("#a").background(“red”)將該對象的background樣式設為red

          jQuery提供了以下方法,來操作css

          background ()   background (val)     color()    color(val)     css(name)    css(prop)   
          css(key, value)      float()   float(val)   height()   height(val)  width()  width(val) 
          left()   left(val)       overflow()   overflow(val)   position()   position(val)  top()   top(val)

          這里需要講解一下css(name)  css(prop)  css(key, value),其他的看名字都知道什么作用了! <div id="a" style="background:blue; color:red">css</div><P id="b">test</P>
          css(name)  獲取樣式名為name的樣式
          $("#a").css("color") 將得到樣式中color值red,("#a").css("background ")將得到blue

          css(prop)  prop是一個hash對象,用于設置大量的css樣式
          $("#b").css({ color: "red", background: "blue" });
          最終效果是<p id="b" style="background:blue; color:red">test</p>,{ color: "red", background: "blue" },hash對象,color為key,"red"為value,

          css(key, value)  用于設置一個單獨得css樣式
          $("#b").css("color","red");最終效果是<p id="b" style="color:red">test</p>

          四:JavaScript處理

          $.browser()  判斷瀏覽器類型,返回boolen值 $(function(){
              if($.browser.msie) {
                  alert("這是一個IE瀏覽器");}
              else if($.browser.opera) {
                  alert("這是一個opera瀏覽器");}
          })當頁面載入式判斷瀏覽器類型,可判斷的類型有msie、mozilla、opera、safari

          $.each(obj, fn)  obj為對象或數(shù)組,fn為在obj上依次執(zhí)行的函數(shù),注意區(qū)分$().each() $.each( [0,1,2], function(i){ alert( "Item #" + i + ": " + this ); });    分別將0,1,2為參數(shù),傳入到function(i)中 $.each({ name: "John", lang: "JS" },  function(i){ alert( "Name: " + i + ", Value: " + this );    { name: "John", lang: "JS" }為一個hash對象,依次將hash中每組對象傳入到函數(shù)中

          $.extend(obj, prop)  用第二個對象擴展第一個對象 var settings = { validate: false, limit: 5, name: "foo" };
          var options = { validate: true, name: "bar" };
          $.extend(settings, options);執(zhí)行后settings對象為{ validate: true, limit: 5, name: "bar" }
          可以用下面函數(shù)來測試 $(function(){
                 var settings = { validate: false, limit: 5, name: "foo" };
                  var options = { validate: true, name: "bar" };
                  $.extend(settings, options);
                  $.each(settings,  function(i){ alert( i + "=" + this ); });
          })
          $.grep(array,fn)  通過函數(shù)fn來過濾array,將array中的元素依次傳給fn,fn必須返回一個boolen,如fn返回true,將被過濾 $(function(){
                  var arr= $.grep( [0,1,2,3,4], function(i){ return i > 2; });
                  $.each(arr, function(i){ alert(i); });
          })我們可以看待執(zhí)行$.grep后數(shù)組[0,1,2,3,4]變成[0,1]

          $.merge(first, second)  兩個參數(shù)都是數(shù)組,排出第二個數(shù)組中與第一個相同的,再將兩個數(shù)組合并 $(function(){
                  var arr = $.merge( [0,1,2], [2,3,4] )
                  $.each(arr,  function(i){ alert(i); });
          })可以看出arr的結果為[0,1,2,3,4]

          $.trim(str)  移出字符串兩端的空格
              $.trim("   hello, how are you?   ")的結果是"hello, how are you?"

           

          五:動態(tài)效果

                 在將這部分之前我們先看個例子,相信做網(wǎng)頁的朋友都遇到n級菜單的情景,但點擊某菜單按鈕時,如果它的子菜單是顯示的,則隱藏子菜單,如果子菜單隱藏,則顯示出來,傳統(tǒng)的javascript做法是先用getElementById取出子菜單所在容器的id,在判斷該容器的style.display是否等于none,如果等于則設為block,如果不等于這設為none,如果在將效果設置復雜一點,當點擊按鈕時,不是忽然隱藏和顯示子菜單,而是高度平滑的轉變,這時就要通過setTimeout來設置子菜單的height了,再復雜一點透明度也平滑的消失和顯現(xiàn),這時顯現(xiàn)下來需要編寫很多代碼,如果js 基礎不好的朋友可能只能從別人寫好的代碼拿過來修改了!jQuery實現(xiàn)上面效果只需要1句話就行,$("#a").toggle("slow"),,學完jQuery后還需要抄襲修改別人的代碼嗎?下面我們逐個介紹jQuery用于效果處理的方法。

          hide()  隱藏匹配對象 <p id="a">Hello Again</p><a href="#" onClick=’ ("#a").hide()’>jQuery</a>當點擊連接時,id為a的對象的display變?yōu)閚one。

          show() 顯示匹配對象

          hide(speed)  以一定的速度隱藏匹配對象,其大小(長寬)和透明度都逐漸變化到0,speed有3級("slow", "normal",  "fast"),也可以是自定義的速度。

          show(speed)  以一定的速度顯示匹配對象,其大小(長寬)和透明度都由0逐漸變化到正常

          hide(speed, callback)  show(speed, callback) 當顯示和隱藏變化結束后執(zhí)行函數(shù)callback

          toggle()    toggle(speed) 如果當前匹配對象隱藏,則顯示他們,如果當前是顯示的,就隱藏,toggle(speed),其大小(長寬)和透明度都隨之逐漸變化。 <img src="1.jpg" style="width:150px"/>
          <a href="#" onClick='$("img").toggle("slow")'>jQuery</a>
          fadeIn(speeds)   fadeOut(speeds)  根據(jù)速度調(diào)整透明度來顯示或隱藏匹配對象,注意有別于hide(speed)和show(speed),fadeIn和fadeOut都只調(diào)整透明度,不調(diào)整大小 <img src="1.jpg" style="display:none"/><a href="#" onClick='$("img ").fadeIn("slow")'> jQuery </a>點擊連接后可以看到圖片逐漸顯示。

          fadeIn(speed, callback)  fadeOut(speed, callback)   callback為函數(shù),先通過調(diào)整透明度來顯示或隱藏匹配對象,當調(diào)整結束后執(zhí)行callback函數(shù) <img src="1.jpg"/>
          <a href="#" onClick='$("img ").fadeIn("slow",function(){ alert("Animation Done."); })'> jQuery </a>點擊連接后可以看到圖片逐漸顯示,顯示完全后彈出對話框

          fadeTo(speed, opacity, callback)  將匹配對象以speed速度調(diào)整倒透明度opacity,然后執(zhí)行函數(shù)callback。Opacity為最終顯示的透明度(0-1). <img src="1.jpg"/><br>
          <a href="#" onClick='$("img ").fadeTo("slow",0.55,function(){ alert("Animation Done."); })'> jQuery </a>大家可以看一下自己看看效果,如果不用jQuery,編寫原始javascript腳本可能很多代碼!

          slideDown(speeds)  將匹配對象的高度由0以指定速率平滑的變化到正常! <img src="1.jpg" style="display:none"/>
          <a href="#" onClick='$("img ").slideDown("slow")'>jQuery</a>
          slideDown(speeds,callback)  將匹配對象的高度由0變化到正常!變化結束后執(zhí)行函數(shù)callback

          slideUp("slow")  slideUp(speed, callback) 匹配對象的高度由正常變化到0

          slideToggle("slow") 如果匹配對象的高度正常則逐漸變化到0,若為0,則逐漸變化到正常 


          六 :事件處理

            hover(Function, Function)    當鼠標move over時觸發(fā)第一個function,當鼠標move out時觸發(fā)第二個function
          樣式:<style>.red{color:#FF0000}</style>
          Html代碼: <div id="a">sdf</div>
          jQuery代碼及效果 $(function(){
            $("#a").hover(function(){$(this).addClass("red");},
                                      function(){ $(this).removeClass("red");
                                    });
          })最終效果是當鼠標移到id為a的層上時圖層增加一個red樣式,離開層時移出red樣式

          toggle(Function, Function)    當匹配元素第一次被點擊時觸發(fā)第一個函數(shù),當?shù)诙伪稽c擊時觸發(fā)第二個函數(shù)
          樣式:<style>.red{color:#FF0000}</style>
          Html代碼: <div id="a">sdf</div>
          jQuery代碼及效果 $(function(){
            $("#a"). toggle (function(){$(this).addClass("red");},
                                        function(){ $(this).removeClass("red");
                                      });
          })最終效果是當鼠標點擊id為a的層上時圖層增加一個red樣式,離開層時移出red樣式

          bind(type, fn)   用戶將一個事件和觸發(fā)事件的方式綁定到匹配對象上。
          trigger(type)   用戶觸發(fā)type形式的事件。$("p").trigger("click")
          還有: unbind()   unbind(type)    unbind(type, fn)
          Dynamic event(Function)    綁定和取消綁定提供函數(shù)的簡捷方式
          例: $("#a").bind("click",function() {
                                                 $(this).addClass("red");
          })也可以這樣寫: $("#a").click(function() {
                                  $(this).addClass("red");
          });
          最終效果是當鼠標點擊id為a的層上時圖層增加一個red樣式,

          jQuery提供的函數(shù)
          用于browers事件
          error(fn)    load(fn)     unload(fn)    resize(fn)    scroll(fn)

          用于form事件
          change(fn)    select(fn)    submit(fn)

          用于keyboard事件
          keydown(fn)    keypress(fn)    keyup(fn)

          用于mouse事件
          click(fn)    dblclick(fn)    mousedown(fn)   mousemove(fn)
          mouseout(fn)  mouseover(fn)     mouseup(fn)

          用于UI事件
          blur(fn)    focus(fn)

          以上事件的擴展再擴展為5類
          舉例,click(fn) 擴展 click()  unclick()  oneclick(fn)  unclick(fn)
          click(fn):增加一個點擊時觸發(fā)某函數(shù)的事件
          click():可以在其他事件中執(zhí)行匹配對象的click事件。
          unclick ():不執(zhí)行匹配對象的click事件。
          oneclick(fn):只增加可以執(zhí)行一次的click事件。
          unclick (fn):增加一個點擊時不觸發(fā)某函數(shù)的事件。
          上面列舉的用于browers、form、keyboard、mouse、UI的事件都可以按以上方法擴展。


                                                  七:Ajax支持

           通用方式:
          $.ajax(prop)    通過一個ajax請求,回去遠程數(shù)據(jù),prop是一個hash表,它可以傳遞的key/value有以下幾種 。
                   (String)type:數(shù)據(jù)傳遞方式(get或post)。
                   ((String)url:數(shù)據(jù)請求頁面的url
                   ((String)data:傳遞數(shù)據(jù)的參數(shù)字符串,只適合post方式
                   ((String)dataType:期待數(shù)據(jù)返回的數(shù)據(jù)格式(例如 "xml", "html", "script",或 "json")
                   ((Boolean)ifModified: 當最后一次請求的相應有變化是才成功返回,默認值是false
                   ((Number)timeout:設置時間延遲請求的時間。可以參考$.ajaxTimeout
                   ((Boolean)global:是否為當前請求觸發(fā)ajax全局事件,默認為true
                   ((Function)error:當請求失敗時觸發(fā)的函數(shù)。
                   ((Function)success:當請求成功時觸發(fā)函數(shù)
                   ((Function)complete:當請求完成后出發(fā)函數(shù)
          jQuery代碼及說明

          $.ajax({url: "ajax.htm",
                    success:function(msg){
                                   $(div"#a").html(msg);
                          }
              });將ajax.htm返回的內(nèi)容作為id為a的div內(nèi)容 $.ajax({ url: "ajax.aspx",
                        type:"get",          
                       dataType:"html",
                       data: "name=John&location=Boston",
                       success:function(msg){
                                           $("#a").html(msg);
                                        }
                   });
          用get方式向ajax.aspx頁面?zhèn)鲄?shù),并將返回內(nèi)容負給id為a的對象。

          $.ajaxTimeout(time) 設置請求結束時間
             $.ajaxTimeout( 5000 )

          其它簡化方式:

          $.get(url, params, callback)  用get方式向遠程頁面?zhèn)鬟f參數(shù),請求完成后處理函數(shù),除了url外,其它參數(shù)任意選擇 !

          $.get( "ajax.htm" , function(data){ $("#a").html(data)  })$.get(   "ajax.asp",
                      { name: "young", age: "25" },
                      function(data){ alert("Data Loaded: " + data); }
                  ) $.getIfModified(url, params, callback)  用get方式向遠程頁面?zhèn)鬟f參數(shù),從最后一次請求后如果數(shù)據(jù)有變化才作出響應,執(zhí)行函數(shù)callback
          $.getJSON(url, params, callback)  用get方式向遠程json對象傳遞參數(shù),請求完成后處理函數(shù)callback。
          $.getScript(url, callback)  用get方式載入并運行一個遠程javascript文件。請求完成后處理函數(shù)callback。
          $.post(url, params, callback)  用post方式向遠程頁面?zhèn)鬟f參數(shù),請求完成后處理函數(shù)callback
          load(url, params, callback)  載入一個遠程文件并載入頁面DOM中,并執(zhí)行函數(shù)callback $("#a").load("ajax.htm", function() { alert("load is done"); } );
          向ajax.htm頁面發(fā)出請求,將返回結果裝入id為a的內(nèi)容中,然后再執(zhí)行函數(shù)callback。
          loadIfModified(url, params, callback)  用get方式向遠程頁面?zhèn)鬟f參數(shù),從最后一次請求后如果數(shù)據(jù)有變化才作出響應,將返回結果載入頁面DOM中,并執(zhí)行函數(shù)callback
          ajaxStart(callback) 當ajax請求發(fā)生錯誤是時執(zhí)行函數(shù)callback
          ajaxComplete(callback)  當ajax請求完成時執(zhí)行函數(shù)callback
          ajaxError(callback)  當ajax請求發(fā)生錯誤時執(zhí)行函數(shù)callback
          ajaxStop(callback)  當ajax請求停止時執(zhí)行函數(shù)callback
          ajaxSuccess(callback)  當ajax請求成功時執(zhí)行函數(shù)callback

            
                                                  八:jQuery插件

               隨著jQuery的廣泛使用,已經(jīng)出現(xiàn)了大量jQuery插件,如thickbox,iFX,jQuery-googleMap等,簡單的引用這些源文件就可以方便的使用這些插件。這里我簡單的介紹一些網(wǎng)址供大家參考,這些網(wǎng)站頭提供了大量的demo,并且使用及其簡單,及時E文不好,也能快速掌握!
              http://jquery.com/plugins     官方推薦
              http://interface.eyecon.ro/demos    效果超級棒,使用更簡單,一定有你喜歡的!
              http://www.dyve.net/jquery/
              http://bassistance.de/jquery-plugins

             還有其它很多插件,大家可以google以下,如果大家發(fā)現(xiàn)好的了,可以留言共享以下!


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 达日县| 安宁市| 安丘市| 敖汉旗| 五大连池市| 龙州县| 策勒县| 奉贤区| 正定县| 湾仔区| 萨迦县| 云安县| 鸡西市| 温泉县| 临海市| 邯郸县| 增城市| 方正县| 澎湖县| 偃师市| 西安市| 东港市| 平顶山市| 新干县| 紫金县| 南川市| 兴和县| 沂水县| 滕州市| 阳谷县| 河池市| 威远县| 上思县| 青州市| 辽阳市| 比如县| 登封市| 花莲县| 夏邑县| 西乌| 长泰县|