我正在尝试在 javascript 类中使用 jquery
的 .hover
方法:
var test = {
init:function(){
$("#elm li").hover(test.showTitle(this), test.hideTitle());
},
showTitle:function(e){
...
},
hideTitle: function(){
...
}
};
$(document).ready(function() {
test.init();
});
但这里 this
指的是类本身,而不是 eventObject
。
如何使用 javascript 类中的 jquery 监听事件?
最佳答案
如果您只是删除括号和 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();
});
关于javascript - 从类方法中使用jquery悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393539/