相关疑难解决方法(0)

Javascript - 箭头在事件处理程序中起作用吗?

我是ES6的新手,并且无法完成这项工作:

$(this) 单击时返回undefined?

dom.videoLinks.click((e) => {
            e.preventDefault();
            console.log($(this));
            var self = $(this),
                url = self.attr(configuration.attribute);

            eventHandlers.showVideo(url);

            // Deactivate any active video thumbs
            dom.videoLinks.filter('.video-selected').removeClass('video-selected');

            // Activate selected video thumb
            self.addClass('video-selected');
        });
Run Code Online (Sandbox Code Playgroud)

但是,如果我改变它所以不是像这样的箭头函数,它按预期工作?:

dom.videoLinks.click(function(e) {
            e.preventDefault();
            console.log(this);
            console.log($(this));
            var self = e.this,
                url = self.attr(configuration.attribute);

            eventHandlers.showVideo(url);

            // Deactivate any active video thumbs
            dom.videoLinks.filter('.video-selected').removeClass('video-selected');

            // Activate selected video thumb
            self.addClass('video-selected');
        });
Run Code Online (Sandbox Code Playgroud)

那么如果我在回调中使用箭头函数,我该怎么做呢?

javascript jquery ecmascript-6

11
推荐指数
2
解决办法
9832
查看次数

标签 统计

ecmascript-6 ×1

javascript ×1

jquery ×1