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");