1.常见的事件
鼠标事件:
click() :单击事件
mouseover(): 鼠标移动
mouseout() :鼠标移出
mouseenter() : 鼠标移入
键盘事件:
keydown(): 按下按键
keyup(): 释放按键
表单事件:
input() : 用户输入
focus() : 获得焦点
blur() : 失去焦点
onchange() :表单元素的内容改变
2.添加事件的方式
1.直接添加:
$('dom').click(function(){
alert('hello word')
})
2.使用 `.bind()` 绑定事件
可以绑定多个事件处理程序到单个元素
$('dom').bind('click','mousemove',function(){
alert('hello word')
})
3.使用 `.delegate()` 绑定事件
delegate()为指定元素的一个或多个事件绑定事件处理函数,可以用来写事件委托。
第一个参数:指定哪些后代元素可以触发绑定的事件。
第二个参数:事件类型。
第三个参数:触发事件执行的回调函数。
$('ul').delegate('li','click',function(){
alert('hello word')
})
4.使用 `.on()` 绑定事件
`.on()` 方法可以接受三个参数。
第一个参数: 事件类型
第二个参数: 可以触发绑定的事件的后代元素 (可省略)
第三个参数: 触发事件执行的回调函数。
$('ul').on('click','li',function(){
alert('hello word')
})
3.解除事件
使用 'off()' 解除事件
可以实现拖拽:
$(".box").on("mousedown",function(){
// 绑定鼠标移动事件
$(document).on("mousemove",function(){
console.log("触发了mousemove事件!!!!!!!!!");
})
})
$(document).on("mouseup",function(){
// 解绑鼠标移动事件
$(document).off("mousemove");
})