在 jQuery 中,大多数 DOM 事件都有一个等效的 jQuery 方法。
页面中指定一个点击事件:
1
$("p").click();
下一步是定义什么时间触发事件。您可以通过一个事件函数实现:
1
2
3
$("p").click(function(){
    // 动作触发后执行的代码!!
});

  
常用的 jQuery 事件方法
$(document).ready()
$(document).ready() 方法允许我们在文档完全加载完后执行函数。该事件方法在jQuery 语法章节中已经提到过。
click()
click() 方法是当按钮点击事件被触发时会调用一个函数。
该函数在用户点击 HTML 元素时执行。
在下面的实例中,当点击事件在某个 <p> 元素上触发时,隐藏当前的 <p> 元素:
1
2
3
$("p").click(function(){
    $(this).hide();
});
dblclick()
当双击元素时,会发生 dblclick 事件。
dblclick() 方法触发 dblclick 事件,或规定当发生 dblclick 事件时运行的函数:
1
2
3
$("p").dblclick(function(){
    $(this).hide();
});
mouseenter()
当鼠标指针穿过元素时,会发生 mouseenter 事件。
mouseenter() 方法触发 mouseenter 事件,或规定当发生 mouseenter 事件时运行的函数。
1
2
3
$("#p1").mouseenter(function(){
    alert("You entered p1!");
});
mouseleave()
当鼠标指针离开元素时,会发生 mouseleave 事件。
mouseleave() 方法触发 mouseleave 事件,或规定当发生 mouseleave 事件时运行的函数:
1
2
3
$("#p1").mouseleave(function(){
    alert("Bye! You now leave p1!");
});
mousedown()
当鼠标指针移动到元素上方,并按下鼠标按键时,会发生 mousedown 事件。
mousedown() 方法触发 mousedown 事件,或规定当发生 mousedown 事件时运行的函数:
1
2
3
$("#p1").mousedown(function(){
    alert("Mouse down over p1!");
});
mouseup()
当在元素上松开鼠标按钮时,会发生 mouseup 事件。
方法触发 mouseup 事件,或规定当发生 mouseup 事件时运行的函数:
1
2
3
$("#p1").mouseup(function(){
    alert("Mouse up over p1!");
});
hover()
hover()方法用于模拟光标悬停事件。
当鼠标移动到元素上时,会触发指定的第一个函数(mouseenter);当鼠标移出这个元素时,会触发指定的第二个函数(mouseleave)。
1
2
3
4
5
6
$("#p1").hover(function(){
    alert("You entered p1!");
    },
    function(){
    alert("Bye! You now leave p1!");
});
focus()
当元素获得焦点时,发生focus事件。
当通过鼠标点击选中元素或通过tab键定位到元素时,该元素就会获得焦点。
focus()方法触发 focus 事件,或规定当发生focus事件时运行的函数。
1
2
3
$("input").focus(function(){
    $(this).css("background-color","#cccccc");
});
blur()
当元素失去焦点时,发生 blur 事件。
blur() 方法触发 blur 事件,或规定当发生 blur 事件时运行的函数:
1
2
3
$("input").blur(function(){
    $(this).css("background-color","#ffffff");
});
posted on 2020-11-22 16:44  吴。。。。。  阅读(81)  评论(0编辑  收藏  举报