jQuery中的事件

來源:互聯網
上載者:User

標籤:jquery   對象   

1、事件綁定     bind(type[,data],fn)

     bind()方法有3個參數:

     a、第1個參數是事件類型,包括 blur,focus,load,resize,scroll,unload,click,dbclick,mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave,change,select,submit,keydown,keypress,keyup,error等,也可以是自訂名稱。

     b、第2個參數為選擇性參數,做完event.data屬性值傳遞給事件對象的額外資料對象。

     c、第3個參數是用來綁定的處理函數。   

$("#panel h5.head").bind("click",function(){});

2、合成事件    hover()和toggle()

     a、hover()類比游標懸停事件

     $("#panel h5.head").hover(function(){         $(this).next().show();     },function(){         $(this).next().hide();     });

      b、toggle()方法基本同上,交替操作

3、事件冒泡--停止事件冒泡--阻止預設行為

     事件冒泡:事件按照DOM的階層像水泡一樣不斷向上直至頂端。 例:body內有個div,div中有個span,三個元素均綁定的有click事件,點擊span,則會彈出3條記錄,依次為內層span被點擊,外層div被點擊,body被點擊,   這就是所謂的冒泡。                             

     a、事件對象:即為函數添加一個參數,jQuery代碼如下:

     $(“element”).bind("click",function(event){          //事件對象          //……      })

     b、停止事件冒泡        

     $(“element”).bind("click",function(event){          //事件對象         var txt=$("#msg").html() + "<p>內層實盤元素被單擊。</p>"         s("#msg").html(txt);         event.stopPropagation();                          //停止事件冒泡     })

     c、阻止預設行為(網頁中的元素有自己預設的行為,比如單擊超連結會跳轉,單擊提交按鈕後,表單會提交,但有時需要阻止元素的預設行為【當表單不符合提交條件時,要阻止表單的提交】)        

     $(“#sub”).bind("click",function(event){          //事件對象        var username=$("#username").val();              //擷取元素的值        if(username==""){                 //判斷值是否為空白            $("#msg").html("<p>文字框內容不可為空!<p>"");            //提示資訊            event.preventDefault();          //阻止預設行為(表單提交)         }                  })

       d、如果想同時對事件對象停止冒泡和預設行為,可以在事件處理函數中返回false。這是對同事調用stopPrapagation()方法和preventDefault()方法的一種簡寫方式。

4、事件對象的屬性

      a、event.type  擷取到事件的類型

     $("#a").click(function(event){        alert(event.type);           //擷取事件類型        return false;            //阻止跳轉連結     });

          代碼運行後,輸出:click

       b、event.preventDefault()  阻止預設的事件行為

       c、event.stopPropagation()   阻止事件的冒泡

       d、event.target     擷取到觸發事件的元素

         $("a[href='http://google.com']").click(function(event){             var tg=event.target;             alert(tg.href);             return false;         });

             代碼運行後輸出:http://google.com

5、event.pageX和event.pageY    擷取到游標相對於頁面的x、y座標。

6、event.which    在按一下滑鼠事件中擷取到滑鼠的左、中、右鍵;在鍵盤事件中擷取鍵盤的按鍵。

        $("a").mousedown(function(e){            alert(e.which);         //1=滑鼠左鍵;2=滑鼠中鍵;3=滑鼠右鍵                      });
        $("a").keyup(function(e){            alert(e.which);         //擷取鍵盤的按鍵;                      });

7、event.metaKey     鍵盤事件中擷取<ctrl>按鍵

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.