非常教程

jQuery参考手册

jQuery 事件

jQuery 事件

jQuery 事件


jQuery 是为事件处理特别设计的。


什么是事件?

页面对不同访问者的响应叫做事件。

事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。

实例:

  • 在元素上移动鼠标。
  • 选取单选按钮
  • 点击元素

在事件中经常使用术语"触发"(或"激发")例如: "当您按下按键时触发 keypress 事件"。

常见 DOM 事件:

鼠标事件 键盘事件 表单事件 文档/窗口事件
click keypress submit load
dblclick keydown change resize
mouseenter keyup focus scroll
mouseleave   blur unload
hover      


jQuery 事件方法语法

在 jQuery 中,大多数 DOM 事件都有一个等效的 jQuery 方法。

页面中指定一个点击事件:

$("p").click();

下一步是定义什么时间触发事件。您可以通过一个事件函数实现:

$("p").click(function(){ // 动作触发后执行的代码!! });


常用的 jQuery 事件方法

$(document).ready()

$(document).ready() 方法允许我们在文档完全加载完后执行函数。该事件方法在 jQuery 语法 章节中已经提到过。

click()

click() 方法是当按钮点击事件被触发时会调用一个函数。

该函数在用户点击 HTML 元素时执行。

在下面的实例中,当点击事件在某个 <p> 元素上触发时,隐藏当前的 <p> 元素:

实例

$("p").click(function(){ $(this).hide(); });

尝试一下 »

dblclick()

当双击元素时,会发生 dblclick 事件。

dblclick() 方法触发 dblclick 事件,或规定当发生 dblclick 事件时运行的函数:

实例

$("p").dblclick(function(){ $(this).hide(); });

尝试一下 »

mouseenter()

当鼠标指针穿过元素时,会发生 mouseenter 事件。

mouseenter() 方法触发 mouseenter 事件,或规定当发生 mouseenter 事件时运行的函数:

实例

$("#p1").mouseenter(function(){ alert('您的鼠标移到了 id="p1" 的元素上!'); });

尝试一下 »

mouseleave()

当鼠标指针离开元素时,会发生 mouseleave 事件。

mouseleave() 方法触发 mouseleave 事件,或规定当发生 mouseleave 事件时运行的函数:

实例

$("#p1").mouseleave(function(){ alert("再见,您的鼠标离开了该段落。"); });

尝试一下 »

mousedown()

当鼠标指针移动到元素上方,并按下鼠标按键时,会发生 mousedown 事件。

mousedown() 方法触发 mousedown 事件,或规定当发生 mousedown 事件时运行的函数:

实例

$("#p1").mousedown(function(){ alert("鼠标在该段落上按下!"); });

尝试一下 »

mouseup()

当在元素上松开鼠标按钮时,会发生 mouseup 事件。

mouseup() 方法触发 mouseup 事件,或规定当发生 mouseup 事件时运行的函数:

实例

$("#p1").mouseup(function(){ alert("鼠标在段落上松开。"); });

尝试一下 »

hover()

hover()方法用于模拟光标悬停事件。

当鼠标移动到元素上时,会触发指定的第一个函数(mouseenter);当鼠标移出这个元素时,会触发指定的第二个函数(mouseleave)。

实例

$("#p1").hover( function(){ alert("你进入了 p1!"); }, function(){ alert("拜拜! 现在你离开了 p1!"); } );

尝试一下 »

focus()

当元素获得焦点时,发生 focus 事件。

当通过鼠标点击选中元素或通过 tab 键定位到元素时,该元素就会获得焦点。

focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数:

实例

$("input").focus(function(){ $(this).css("background-color","#cccccc"); });

尝试一下 »

blur()

当元素失去焦点时,发生 blur 事件。

blur() 方法触发 blur 事件,或规定当发生 blur 事件时运行的函数:

实例

$("input").blur(function(){ $(this).css("background-color","#ffffff"); });

尝试一下 »


jQuery 事件

jQuery目录

1.jQuery 教程
2.jQuery 简介
3.jQuery 安装
4.jQuery 语法
5.jQuery 选择器
6.jQuery 效果 – 隐藏和显示
7.jQuery 事件
8.jQuery 效果 – 滑动
9.jQuery 效果 – 淡入淡出
10.jQuery 链
11.jQuery Callback 方法
12.jQuery 效果 – 停止动画
13.jQuery 效果 – 动画
14.jQuery 遍历 – 后代
15.jQuery 遍历 – 祖先
16.jQuery 遍历
17.jQuery 尺寸
18.jQuery css() 方法
19.jQuery 获取并设置 CSS 类
20.jQuery 删除元素
21.jQuery 添加元素
22.jQuery 设置内容和属性
23.jQuery 获取内容和属性
24.jQuery 实例
25.jQuery noConflict() 方法
26.jQuery – AJAX get() 和 post() 方法
27.jQuery – AJAX load() 方法
28.jQuery AJAX 简介
29.jQuery 遍历 – 过滤
30.jQuery 遍历 – 同胞(siblings)
31.jQuery :even 选择器
32.jQuery :last 选择器
33.jQuery :first 选择器
34.jQuery 多个元素选择器
35.jQuery element 选择器
36.jQuery 多个类选择器
37.jQuery .class 选择器
38.jQuery #id 选择器
39.jQuery * 选择器
40.jQuery :only-child 选择器
41.jQuery :nth-last-of-type() 选择器
42.jQuery :nth-of-type() 选择器
43.jQuery :nth-last-child() 选择器
44.jQuery :nth-child() 选择器
45.jQuery :last-of-type 选择器
46.jQuery :last-child 选择器
47.jQuery :first-of-type 选择器
48.jQuery :first-child 选择器
49.jQuery :odd 选择器
50.jQuery :header 选择器
51.jQuery :not() 选择器
52.jQuery :lt() 选择器
53.jQuery :gt() 选择器
54.jQuery :eq() 选择器
55.jQuery element ~ siblings 选择器
56.jQuery element + next 选择器
57.jQuery 父后代选择器
58.jQuery parent > child 选择器
59.jQuery :only-of-type 选择器
60.jQuery :lang() 选择器
61.jQuery :root 选择器
62.jQuery :visible 选择器
63.jQuery :hidden 选择器
64.jQuery :parent 选择器
65.jQuery :empty 选择器
66.jQuery :has() 选择器
67.jQuery :contains() 选择器
68.jQuery :focus 选择器
69.jQuery :animated 选择器
70.jQuery :input 选择器
71.jQuery [attribute*=value] 选择器
72.jQuery [attribute~=value] 选择器
73.jQuery [attribute^=value] 选择器
74.jQuery [attribute|=value] 选择器
75.jQuery [attribute$=value] 选择器
76.jQuery [attribute!=value] 选择器
77.jQuery [attribute=value] 选择器
78.jQuery [attribute] 选择器
79.jQuery :image 选择器
80.jQuery :button 选择器
81.jQuery :reset 选择器
82.jQuery :submit 选择器
83.jQuery :checkbox 选择器
84.jQuery :radio 选择器
85.jQuery :password 选择器
86.jQuery :text 选择器
87.jQuery dblclick() 方法
88.jQuery click() 方法
89.jQuery change() 方法
90.jQuery blur() 方法
91.jQuery bind() 方法
92.jQuery :checked 选择器
93.jQuery :selected 选择器
94.jQuery :disabled 选择器
95.jQuery :enabled 选择器
96.jQuery :file 选择器
97.jQuery event.namespace 属性
98.jQuery event.isPropagationStopped() 方法
99.jQuery event.isImmediatePropagationStopped() 方法
100.jQuery event.isDefaultPrevented() 方法