当前位置: 代码迷 >> Web前端 >> jQuery-bind不能处置绑定hover事件
  详细解决方案

jQuery-bind不能处置绑定hover事件

热度:373   发布时间:2012-11-16 14:12:15.0
jQuery-bind不能处理绑定hover事件
看这个例子: $(document).ready(function(){ $('.some-class').bind({ hover: function(e) { // Hover event handler alert("hover"); }, click: function(e) { // Click event handler alert("click"); }, blur: function(e) { // Blur event handler } }); }); 奇怪的事情发生了,这里的 “hover” 事件完全没有反应。 而...
看这个例子:
$(document).ready(function(){
$('.some-class').bind({
hover: function(e) {
// Hover event handler
alert("hover");
},
click: function(e) {
// Click event handler
alert("click");
},
blur: function(e) {
// Blur event handler
}
});
});
奇怪的事情发生了,这里的 “hover” 事件完全没有反应。 而像 “click” 和 “blur” 这两个都能正常调用。
同时,如下的代码也是正常运行的:
$(".some-class").hover(function(){
// stuff
})
为什么 bind 不能绑定 hover 呢?
答案是:
应该使用 mouseenter 和 mouseleave 这两个事件来代替 (这也是 .hover() 函数中使用的事件) 所以完全可以直接像这样来引用:
$(document).ready(function(){
$('.some-class').bind({
mouseenter: function(e) {
// Hover event handler
alert("hover");
},
mouseleave: function(e) {
// Hover event handler
alert("hover");
},
click: function(e) {
// Click event handler
alert("click");
},
blur: function(e) {
// Blur event handler
}
});
});
因为 .hover() 是 jQuery 自己定义的事件… 是为了方便用户绑定调用 mouseenter 和 mouseleave 事件而已,它并非一个真正的事件,所以当然不能当做 .bind() 中的事件参数来调用。
http://www.jdfwkey.com/
  相关解决方案