Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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上事件的关键字_Javascript_Jquery - Fatal编程技术网

通过';这';面向对象Javascript上事件的关键字

通过';这';面向对象Javascript上事件的关键字,javascript,jquery,Javascript,Jquery,我不熟悉javascript/jQuery 我正在做一个简单的淡入淡出滑块来在页面上使用,并且我试图在对象内部尽可能多地放置,因此每当我创建对象的新实例时,它都可以开箱即用。 在设置导航时,我首先执行了以下操作: testimonial = new slider($('.testimonials'), $('.testimonials-nav')); testimonial.nav.on('click', function (e) { testimonial.setCurrent($(this)

我不熟悉javascript/jQuery

我正在做一个简单的淡入淡出滑块来在页面上使用,并且我试图在对象内部尽可能多地放置,因此每当我创建对象的新实例时,它都可以开箱即用。 在设置导航时,我首先执行了以下操作:

testimonial = new slider($('.testimonials'), $('.testimonials-nav'));
testimonial.nav.on('click', function (e) {
testimonial.setCurrent($(this).data('dir')); // changes the current slider
testimonial.transition(); // what happens when the 'current' changes
testimonial.setHeight(); //because everything is 'position: absolute', I set the parent div height with js
e.preventDefault();
});
但那样的话,每次创建滑块的新实例时,我都需要重复所有这些代码,所以我尝试了一种不同的方法,我创建了两种方法:

slider.prototype.init = function () {
    this.numberEls();
    this.setHeight();
    this.activateNav();
};
因此,当我创建一个新实例时,我可以执行以下操作

testimonial = new slider($('.testimonials'), $('.testimonials-nav'));
testimonial.init();
并创建了这个方法

slider.prototype.activateNav = function () {
    this.nav.on('click', function (e) {        
        this.setCurrent($(this).data('dir'));
        e.preventDefault();
    });
};
但由于事件处理程序中“this”关键字的作用域,它显然不起作用,我如何使“this.setCurrent”在这种上下文中起作用

总的来说,另一个问题是,这是否是实现我的目标的好方法?请记住,它可能用于网站的其他部分,而不仅仅是在推荐信中

如果要求不太多,我怎么能让它自动循环?我知道它涉及setTimeOut()或其他什么,但我不知道确切的方法(我需要诚实地说,这是一个懒惰的问题,我甚至在问如何做之前都没有尝试过,但因为我在这里=P)


注:如果我没有说清楚,我很抱歉,英语不是我的第一语言,有时我会与之斗争

slider.prototype.activateNav = function () {
    var that = this;
    this.nav.on('click', function (e) {        
        that.setCurrent($(this).data('dir'));
        e.preventDefault();
    });
};

我已经试过了,但我不知道为什么不行哇,我成功了。它实际上是有效的,我只是没有调用event=P上的转换。谢谢,我不认为这是重复的,因为问题不是您对“this”对象的具体处理,而是在同一范围内使用JQuery“this”和对象“this”的结果。尝试将Jquery对象传递到activateNav方法中,那么原型方法中的“this”引用就可以了。对不起,“将Jquery对象传递到activateNav方法中”是什么意思?看看这是否有帮助: