关于事件(常用)

2018-08-08  本文已影响7人  就那两三事

焦点

$(selector).focus(function)

当元素获得焦点时(当通过鼠标点击选中元素或通过 tab 键定位到元素时),发生 focus 事件

$(selector).blur(function)

表单

$(selector).change(function)

当元素的值改变时发生 change 事件(仅适用于表单字段)
注意:当用于 select 元素时,change 事件会在选择某个选项时发生。当用于 text field 或 text area 时,change 事件会在元素失去焦点时发生。

键盘

与 keydown 事件相关的事件顺序:
1、 keydown - 键按下的过程
2、keypress - 键被按下
3、keyup - 键被松开

$(selector).keydown(function)

当键盘键被按下时发生 keydown 事件
提示:使用event.which属性来返回哪个键盘键被按下

$(selector).keypress(function)

keypress 事件不会触发所有的键(比如 ALT、CTRL、SHIFT、ESC)

$(selector).keyup(function)

鼠标

$(selector).click(function) 点击事件

$(selector).dbclick(function) 双击事件

dblclick 事件也会产生 click 事件。如果这两个事件都被应用于同一个元素,则会产生问题。

$(selector).mouseover(function) 悬停事件

与 mouseenter事件不同,mouseover 事件在鼠标指针进入被选元素或任意子元素时都会被触发,mouseenter 事件只有在鼠标指针进入被选元素时被触发。

$(selector).mouseout(function) 离开事件

与mouseleave事件不同,mouseout 事件在鼠标指针离开被选元素或任意子元素时都会被触发,mouseleave 事件只有在鼠标指针离开被选元素时被触发。

$(selector).mouseenter(function) 进入事件

$(selector).mouseleave(function) 离开事件

$(selector).mousedown(function) 鼠标按下事件

$(selector).mouseup(function) 鼠标松开事件

$(selector).mousemove(function) 鼠标移动事件

用户把鼠标移动一个像素,就会发生一次 mousemove 事件。处理所有 mousemove 事件会耗费系统资源。请谨慎使用该事件。

$(selector).hover(inFunc,outFunc)

规定当鼠标指针悬停在被选元素上时要运行的两个函数。
inFunc:必需,mouseover 事件发生时运行的函数。
outFunc:可选,mouseout 事件发生时运行的函数。

$( selector ).hover( handlerIn, handlerOut )

等价于:$( selector ).mouseover( handlerIn ).mouseout( handlerOut )

如果只有了一个函数,则它将会在 mouseover 和 mouseout 事件上运行,即
$(selector).hover(handlerInOut)
等价于: $( selector ).on( "mouseover mouseout", handlerInOut )

jQuery 1.7 版本前该方法触发 mouseenter 和 mouseleave 事件。
jQuery 1.8 版本后该方法触发 mouseover 和 mouseout 事件。

$(selector).toggle(function)

toggle() 方法在 jQuery 版本 1.8 中被废弃,在版本 1.9 中被移除。
toggle() 方法添加两个或多个函数,以响应被选元素的 click 事件之间的切换。
当在元素上点击时调用第一个指定函数,当再次点击时调用第二个函数,以此类推。

上一篇 下一篇

猜你喜欢

热点阅读