Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jqueryhover from类方法_Javascript_Jquery_Class_Events_Hover - Fatal编程技术网

Javascript 使用jqueryhover from类方法

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

我试图在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();
});
但是这里的
这个
指的是类本身,而不是
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();
});

您是否尝试绑定而不是直接悬停?是否尝试绑定而不是直接悬停?