六:事件处理
hover(Function, Function) 当鼠标move over时触发第一个function,当鼠标move out时触发第二个function
样式:
Html代码: sdf
jQuery代码及效果
$(function(){
$("#a").hover(function(){$(this).addClass("red");},
function(){ $(this).removeClass("red");
});
})
最终效果是当鼠标移到id为a的层上时图层增加一个red样式,离开层时移出red样式
toggle(Function, Function) 当匹配元素第一次被点击时触发第一个函数,当第二次被点击时触发第二个函数
样式:
Html代码: sdf
jQuery代码及效果
$(function(){
$("#a"). toggle (function(){$(this).addClass("red");},
function(){ $(this).removeClass("red");
});
})
The final effect is that when the mouse clicks on the layer with id a, the layer adds a red style, and when leaving the layer, the red style is removed
bind(type, fn) The user will The event and the way the event is triggered are bound to the matching object.
trigger(type) The user triggers an event in the form of type. $("p").trigger("click")
Also:unbind() unbind(type) unbind(type, fn)
Dynamic event(Function) A shortcut way to bind and unbind provided functions
Example:
$("#a").bind(" click",function() { 🎜>
).addClass("red");}) can also be written like this :
$(
"
#a").click(function () { 🎜>);});
The final effect is that when the mouse clicks on the layer with id a, the layer adds a red style. The function provided by jQuery is used for browsers eventerror(fn) load(fn) unload(fn) resize(fn) scroll(fn)
for form event
change(fn) select(fn) submit(fn)
for keyboard event
keydown
(fn) keypress(fn) keyup(fn)
for mouse event
click(fn) dblclick(fn) mousedown(fn ) mousemove(fn)
mouseout(fn) mouseover(fn) mouseup(fn)
Used for UI eventsblur(fn) focus(fn)
The expansion of the above events is further expanded to 5 categories
For example, click(fn) extends click() unclick() oneclick(fn) unclick(fn)click(fn): Add an event that triggers a function when clickedclick(): Matching can be performed in other events The click event of the object.
unclick (): Do not execute the click event of the matching object. oneclick(fn): Only add a click event that can be executed once.
unclick (fn): Add an event that does not trigger a function when clicked. The events listed above for browsers, forms, keyboards, mice, and UI can all be expanded according to the above methods.