jQuery--事件總結

來源:互聯網
上載者:User

標籤:style   color   io   ar   資料   div   問題   cti   sp   


標準的綁定:
bind(type,[,data],fn)==>第一個參數是事件類型 第二個選擇性參數作為event.data 傳遞給事件對象的額外資料對象 第三個參數為用來綁定的處理函數
簡寫綁定事件:
$(‘#panel h5.head‘).mouseover(function(){

});
合成事件:
hover(enter,leave) hover事件用來類比滑鼠的懸停事件 $(‘.head‘).hover(function(){},function(){})
toggle(fn1,fn2,fn3....fnN) $(‘.head‘).toggle(function(){},function(){}) toggle函數還有一個作用 切換元素的可見狀態 $(‘p‘).toggle();

事件冒泡:
<div><p><span></span></p></div>例如 在div中添加了點擊事件 p也添加了點擊事件 span 也添加了點擊事件當點擊span標籤時候會觸發三個click事件
這個現象就是事件冒泡
事件冒泡導致的此問題jquery提供瞭解決方案: event.stopPropagation();

事件對象的屬性:
event.type() 擷取到事件的類型
event.preventDefault 組織瀏覽器的預設事件
event.stopPropagation 阻止事件冒泡
event.target 擷取事件來源
event.pageX()/event.pageY()==>擷取游標相對於頁面的x座標和y座標
event.which() ==>擷取作用滑鼠的哪個鍵點擊的 1.左鍵 2.中鍵 3.右鍵
移除事件:
unbind $(‘p‘).unbind(‘click‘);
one(type,[,data],fn) one事件提供了一種方式 一次觸發事件後刪除
類比操作:
$(‘#btn‘).trigger(‘click‘);
其他動作:
同時綁定多個事件類型:
$(‘div‘).bind(‘mouseover mouseout‘,function(){
$(this).toggleClass(‘over‘);
})
添加命名空間:
$(‘div‘).bind(‘click.plugin‘,function(){})
$(‘div‘).bind(‘mouseover.plugin‘,function(){})
$(‘div‘).bind(‘dbclick‘,function(){})
$(‘button‘).click(function(){
$(‘div‘).unbind(‘.plugin‘);
})
事件類型後面添加命名空間,這樣刪除事件的時候只需要刪除命名空間就可以了
觸發命名空間的事件:
$(div).trigger(‘click‘); ==>這樣有沒有命名空間都可以觸發
$(div).trigger(‘click!‘); ==>這樣有歎號的只能觸發不帶有命名空間的事件

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.