jQuery 是为事件处理特别设计的。
什么是事件?页面对不同访问者的响应叫做事件。
事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。
实例:
在元素上移动鼠标。 选取单选按钮 点击元素在事件中经常使用术语"触发"(或"激发")例如: "当您按下按键时触发 keypress 事件"。
常见 DOM 事件:
鼠标事件 | 键盘事件 | 表单事件 | 文档/窗口事件 |
---|---|---|---|
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");
});
相关文章
Noella
2022-11-06
Cytheria
2022-11-06
Damara
2022-11-06
Noya
2022-11-06
Willow
2022-11-06
Endora
2022-11-06
Gaia
2022-11-06
Kamiisa
2022-11-09
Glory
2022-11-09
Xenia
2022-11-09
Winona
2022-11-09
Xandy
2022-11-09
Pandora
2023-02-12
Tanisha
2023-02-12
Wanda
2023-02-26
Miette
2023-04-26
Diane
2023-05-02
Kande
2023-05-02
|