六:事件处理
hover(Function, Function) 当鼠标move over时触发第一个function,当鼠标move out时触发第二个function
样式:<style>.red{color:#FF0000}</style>
Html代码: <div id=”a”>sdf</div>
jQuery代码及效果
$(function(){
$(“#a”).hover(function(){$(this).addClass(“red”);},
function(){ $(this).removeClass(“red”);
});
})最终效果是当鼠标移到id为a的层上时图层增加一个red样式,离开层时移出red样式
toggle(Function, Function) 当匹配元素第一次被点击时触发第一个函数,当第二次被点击时触发第二个函数
样式:<style>.red{color:#FF0000}</style>
Html代码: <div id=”a”>sdf</div>
jQuery代码及效果
$(function(){
$(“#a”). toggle (function(){$(this).addClass(“red”);},
function(){ $(this).removeClass(“red”);
});
})最终效果是当鼠标点击id为a的层上时图层增加一个red样式,离开层时移出red样式
bind(type, fn) 用户将一个事件和触发事件的方式绑定到匹配对象上。
trigger(type) 用户触发type形式的事件。$(“p”).trigger(“click”)
还有:unbind() unbind(type) unbind(type, fn)
Dynamic event(Function) 绑定和取消绑定提供函数的简捷方式
例:
$(“#a”).bind(“click”,function() {
$(this).addClass(“red”);
})也可以这样写:
$(“#a”).click(function() {
$(this).addClass(“red”);
});
最终效果是当鼠标点击id为a的层上时图层增加一个red样式,










