Javascript 使用jqueryhover from类方法
我试图在javascript类中使用Javascript 使用jqueryhover from类方法,javascript,jquery,class,events,hover,Javascript,Jquery,Class,Events,Hover,我试图在javascript类中使用jquery的.hover方法: var test = { init:function(){ $("#elm li").hover(test.showTitle(this), test.hideTitle()); }, showTitle:function(e){ ... }, hideTitle: function(){ ... } }; $(documen
jquery
的.hover
方法:
var test = {
init:function(){
$("#elm li").hover(test.showTitle(this), test.hideTitle());
},
showTitle:function(e){
...
},
hideTitle: function(){
...
}
};
$(document).ready(function() {
test.init();
});
但是这里的这个
指的是类本身,而不是eventObject
如何使用jquery从javascript类侦听事件?您应该将其应用于类查询返回的每个元素
$("#elm li").each(function() {
var $self = $(this);
$self.hover(test.showTitle($self), test.hideTitle());
}
您应该将其应用于类查询返回的每个元素
$("#elm li").each(function() {
var $self = $(this);
$self.hover(test.showTitle($self), test.hideTitle());
}
将调用封装在一个匿名函数中,该函数允许您访问
e
#("elm li").hover(function(e) {
test.showTitle(e); //or this
}, function(e) {
test.hideTitle(e); //or this
});
将调用封装在一个匿名函数中,该函数允许您访问
e
#("elm li").hover(function(e) {
test.showTitle(e); //or this
}, function(e) {
test.hideTitle(e); //or this
});
如果只删除括号和
this
关键字,jQuery将按照元素上下文中的预期使用所需变量调用函数
var test = {
init:function(){
$("#elm li").hover(test.showTitle, test.hideTitle);
},
showTitle:function(e){
console.log(e)
console.log(this);
},
hideTitle: function(){
}
};
$(document).ready(function() {
test.init();
});
如果只删除括号和
this
关键字,jQuery将按照元素上下文中的预期使用所需变量调用函数
var test = {
init:function(){
$("#elm li").hover(test.showTitle, test.hideTitle);
},
showTitle:function(e){
console.log(e)
console.log(this);
},
hideTitle: function(){
}
};
$(document).ready(function() {
test.init();
});
您是否尝试绑定而不是直接悬停?是否尝试绑定而不是直接悬停?