jquery提供了許多的事件處理函數,下面對其總結一下,梳理一下知識點,便於記憶和使用。css
$div = $("div") $div.click(data,function (event) { //點擊盒子變藍 $(this).css({ "background": "blue", }); console.log(event); })
擴展:html
//event參數能夠獲取事件的各類屬性,有幾個經常使用 event.target: 獲取觸發事件的元素 $div.click(function (event) { $(event.target).css({ "background": "blue", }); }) event.data: 獲取事件傳入的參數數據。 event.pageX: 獲取鼠標光標點擊距離文檔左邊left的距離; event.pageY: 獲取鼠標光標點擊距離文檔上邊top的距離; event.offsetX: 獲取鼠標光標點擊距離元素左邊left的距離; event.offssetY: 獲取鼠標光標點擊距離元素上邊top的距離; event.screenX: 獲取鼠標光標點擊距離屏幕left的距離; event.screenY: 獲取鼠標光標點擊距離屏幕top的距離;
$div = $("div") $div.dblclick()(function (event) { //雙擊盒子變藍 $(this).css({ "background": "blue", }); })
$div = $("div") $div.mouseover(function (event) { $(this).css({ "background": "blue", }); }) $div.mouseout(function (event) { $(this).css({ "background": "blue", }); })
$div = $("div") $div.mouseenter(function (event) { $(this).css({ "background": "blue", }); }) $div.mouseleave(function (event) { $(this).css({ "background": "blue", }); })
$div = $("div") // 鼠標進入和移出事件 $div.hover(function (event) { $div.css({ "background": "blue", }) },function (event) { $div.css({ "background": "red", }); })
$div = $("div") $div.mousedown(function (event) { $(this).css({ "background": "blue", }); console.log(event); }) $div.mouseup(function (event) { $(this).css({ "background": "blue", }); console.log(event); })
$(window).keypress([20],function (event) { $div.css({ "background": "blue", }); console.log(event.which); })
注意:若是按下不放開,事件會連續觸發。jquery
$(window).keydown([20],function (event) { $div.css({ "background": "blue", }); console.log(event); }) $(window).keyup([20],function (event) { $div.css({ "background": "blue", }); console.log(event); })
$put = $("input"); $put.focus():元素自動獲取焦點 $put.focus(function (event) { console.log(event); $div.css({ "background": "blue", }) })//獲取焦點後觸發事件 $put.blur(function (event) { console.log(event); $div.css({ "background": "blue", }) })//失去焦點後觸發事件
$(".form").submit(function (event) { alert("提交事件"); console.log(event); //阻止系統默認事件 event.defaultPrevented(); return false; })
$(window).resize(function () { console.log($(window).width()); })
$put.change(function () { $div.css({ "background": "blue", }); })
$(document).unload(function () { $div.css({ "background": "blue", }); console.log("likai"); })
$(function(){ $('div').bind('mouseover click', function(event) { alert($(this).html()); }); });
$(function(){ $('#div1').bind('mouseover click', function(event) { // $(this).unbind();解綁全部事件 $(this).unbind('mouseover');解綁指定事件 }); });
$("div").on(event,childSelector,data,function); //四個參數 $(function(){ $('div').on('mouseover click', function(event) { $(this).css({ "background": "blue", }); }); });
$(function(){ $('#div1').on('mouseover click', function(event) { // $(this).off();解綁全部事件 $(this).off('mouseover');解綁指定事件 }); });
若是須要觸發事件一次後就自動失效,好比:按鈕點擊一次後 就失效使用這個方法。數組
$(function(){ $('div').one('mouseover click', function(event) { $(this).css({ "background": "blue", }); }); });
說明:對於系統沒有提供的事件,能夠本身定義並主動觸發。瀏覽器
$div.bind("abc",function () { $(this).css({ "background": "blue", }); }) $div.trigger("abc");
$(function(){ var $box1 = $('.father'); var $box2 = $('.son'); var $box3 = $('.grandson'); $box1.click(function() { alert('father'); }); $box2.click(function() { alert('son'); }); $box3.click(function(event) { alert('grandson'); // event.stopPropagation();阻止冒泡 }); $(document).click(function(event) { alert('grandfather'); }); }) ...... <div class="father"> <div class="son"> <div class="grandson"></div> </div> </div>
擴展:合併阻止操做dom
event.stopPropagation();//阻止冒泡 event.preventDefault();//阻止默認行爲 // 合併寫法: return false;
利用冒泡原理,將要處理相同事件的子元素的事件委託給父級,從而極大減小事件綁定的次數,提升性能。異步
委託事件:函數
$(function(){ $list = $('list'); $list.delegate('li', 'click', function(event) { $(this).css({background:'red'}); }); })//給列表下面的每一個li元素的事件委託給list列表。
參數:第一個參數是須要委託的元素,採用css選擇器的寫法,默認從一級子元素開始;第二個參數時要委託的事件,能夠是多個,之間用空格隔開,第三個參數是處理函數。性能
event指觸發事件的那個對象。this
$(function(){ $list = $('list'); $list.on('click', 'li', function(event) { $(this).css({background:'red'}); }); })//給列表下面的每一個li元素的事件委託給list列表。
$(function(){ $('div').one('click',"li" function(event) { $(this).css({ "background": "blue", }); }); });
說明:參數用法和on事件同樣。
$list.undelegate();//選擇器找到委託對象取消委託
$list.off("click","li");