标签:页面 load() code 分享 相关 改变 处理 selector 使用
jquery提供了许多的事件处理函数,下面对其总结一下,梳理一下知识点,便于记忆和使用。
$div = $("div") $div.click(data,function (event) { //点击盒子变蓝 $(this).css({ "background": "blue", }); console.log(event); })
$div = $("div") $div.click(data,function (event) { //点击盒子变蓝 $(this).css({ "background": "blue", }); console.log(event); })
扩展:
//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的距离;
扩展2:外层加个function函数表示,只有dom结构树全部加载完之后,再执行jquery代码,这样代码可以放在开头了
<body> <script> $(function () { $div.click(data,function (event) { //点击盒子变蓝 $(this).css({ "background": "blue", }); console.log(event); }) }) </script> <div>1111</div> </body>
2. dblclick():鼠标双击事件
$div = $("div") $div.dblclick()(function (event) { //双击盒子变蓝 $(this).css({ "background": "blue", }); })
3. 鼠标进入和离开(进入子元素也触发)
$div = $("div") $div.mouseover(function (event) { $(this).css({ "background": "blue", }); }) $div.mouseout(function (event) { $(this).css({ "background": "blue", }); })
4. 鼠标进入和离开(进入子元素不触发)
$div = $("div") $div.mouseenter(function (event) { $(this).css({ "background": "blue", }); }) $div.mouseleave(function (event) { $(this).css({ "background": "blue", }); })
5. hover():同时为mouseenter和mouseleave事件指定处理函数
$div = $("div") // 鼠标进入和移出事件 $div.hover(function (event) { $div.css({ "background": "blue", }) },function (event) { $div.css({ "background": "red", }); })
6. 鼠标按下和松开
$div = $("div") $div.mousedown(function (event) { $(this).css({ "background": "blue", }); console.log(event); }) $div.mouseup(function (event) { $(this).css({ "background": "blue", }); console.log(event); })
7. mousemove() 鼠标在元素内部移动
*keypress():按下键盘(指的是按下)
$(window).keypress([20],function (event) { $div.css({ "background": "blue", }); console.log(event.which); })
注意:如果按下不放开,事件会连续触发。
*按下和松开
$(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", }) })//失去焦点后触发事件
* submit(): 用户递交表单
$(".form").submit(function (event) { alert("提交事件"); console.log(event); //阻止系统默认事件 event.defaultPrevented(); return false; })
* ready():DOM加载完成后执行
* resize():浏览器窗口的大小发生改变触发事件
$(window).resize(function () { console.log($(window).width()); })
* scroll():滚动条的位置发生变化
* change(): 表单元素的值发生变化
$put.change(function () { $div.css({ "background": "blue", }); })
* unload() :用户离开页面
$(document).unload(function () { $div.css({ "background": "blue", }); console.log("likai"); })
* bind():绑定
$(function(){ $(‘div‘).bind(‘mouseover click‘, function(event) { alert($(this).html()); }); });
* unbind():解绑
$(function(){ $(‘#div1‘).bind(‘mouseover click‘, function(event) { // $(this).unbind();解绑所有事件 $(this).unbind(‘mouseover‘);解绑指定事件 }); });
* on():绑定和委托都可用的方法
$("div").on(event,childSelector,data,function); //四个参数 $(function(){ $(‘div‘).on(‘mouseover click‘, function(event) { $(this).css({ "background": "blue", }); }); });
* off():解绑
$(function(){ $(‘#div1‘).on(‘mouseover click‘, function(event) { // $(this).off();解绑所有事件 $(this).off(‘mouseover‘);解绑指定事件 }); });
* one():绑定一次自动解绑
如果需要触发事件一次后就自动失效,比如:按钮点击一次后 就失效使用这个方法。
$(function(){ $(‘div‘).one(‘mouseover click‘, function(event) { $(this).css({ "background": "blue", }); }); });
说明:对于系统没有提供的事件,可以自己定义并主动触发。
$div.bind("abc",function () { $(this).css({ "background": "blue", }); }) $div.trigger("abc");
1. 事件的冒泡:
注意:冒泡是事件的固有属性(自定义不适用),适合所有的系统事件。
$(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>
扩展:合并阻止操作
event.stopPropagation();//阻止冒泡 event.preventDefault();//阻止默认行为 // 合并写法: return false;
2. 事件委托
定义:
利用冒泡原理,将要处理相同事件的子元素的事件委托给父级,从而极大减少事件绑定的次数,提高性能。
委托事件:
$(function(){ $list = $(‘list‘); $list.delegate(‘li‘, ‘click‘, function(event) { $(this).css({background:‘red‘}); }); })//给列表下面的每个li元素的事件委托给list列表。
参数:第一个参数是需要委托的元素,采用css选择器的写法,默认从一级子元素开始;第二个参数时要委托的事件,可以是多个,之间用空格隔开,第三个参数是处理函数。
event指触发事件的那个对象。
$(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();//选择器找到委托对象取消委托
off():
$list.off("click","li");
总结:
补充实例:
动态给ul添加li标签的时候,li标签原来已经绑定了click事件,但新添加的标签click不起作用,那么需要委托绑定delegate()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input class="btn" type="button" value="添加"> <ul> <li>11</li> <li>22</li> <li>33</li> </ul> <script src="jquery.js"></script> <script> $(function () { $(".btn").click(function () { $("ul").append("<li>88</li>") }); $("li").click(function () { alert(‘123‘) }) }); </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input class="btn" type="button" value="添加"> <ul> <li>11</li> <li>22</li> <li>33</li> </ul> <script src="jquery.js"></script> <script> $(function () { $(".btn").click(function () { $("ul").append("<li>88</li>") }); $("ul").delegate(‘li‘,‘click‘,function () { //将所有的li标签委托绑定给ul alert(‘123‘) }) }); </script> </body> </html>
【html、CSS、javascript-10】jquery-事件使用方法总结
标签:页面 load() code 分享 相关 改变 处理 selector 使用
原文地址:https://www.cnblogs.com/sunshuhai/p/9129931.html