不能使用.bind()绑定悬停 [英] cannot use .bind() to bind hover
问题描述
我正在尝试使用jQuery.在尝试时,我发现无法将.bind与悬停事件一起使用.而且我不知道出什么问题了.
I experimenting with jQuery. As I was trying I found out that I can't use hover event with .bind. And I don't know what is wrong.
$(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
}
});
});
(至少对我来说)令人惊讶的是,悬停无法正常工作.其他的"click"和"blur"工作正常.
What is surprising (at least to me) is that hover is not working. The others "click" and "blur" are working fine.
以下内容也没有任何问题.
Also the following works without any problems.
$(".some-class").hover(function(){
// stuff
})
也许我可以使用上面的代码.但是不知道为什么会造成很大的麻烦.有什么想法吗?
Maybe I can use the above code. But not knowing why is a big nuisance. So any ideas?
谢谢!
推荐答案
您需要使用mouseenter
和mouseleave
事件(其中 .hover()
在与这样的对象绑定时直接使用):
You need to use the mouseenter
and mouseleave
events (which .hover()
uses) directly when binding with an object like this:
$(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()
.bind()
,因为这不是事件,它只是帮助您绑定mouseenter
和mouseleave
事件.
.hover()
is defined specially here in the jQuery event code...it simply isn't supported like other events in places like .bind()
, since it's not an event, it's just a function to help you bind the mouseenter
and mouseleave
events.
这篇关于不能使用.bind()绑定悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!