ES6 中使用 jQuery $(this) 的问题

栏目: jQuery · 发布时间: 6年前

内容简介:在老项目中开始改用遇到这个问题主要是没有搞清楚转换为了:

在老项目中开始改用 laravel-mix ES6 逐渐过渡。摸索中遇到在与 jQuery 一同使用时 箭头函数$(this) 的含义发生了变化。

遇到这个问题主要是没有搞清楚 箭头函数

$('.js-bottom-btn').click(() => {
    let flag = $(this).date('flag');
    ...
});

转换为了:

$('.js-bottom-btn').click(function() {
    let flag = $(_this).date('flag');
    ...
});

_this is undefined

根据 jQuery click 文档 可以修改为:

$('.js-bottom-btn').click(event => {
    let flag = $(event.currentTarget).date('flag');
    ...
});

类似的问题:

$('jquery-selector').each(() => {
    $(this).click();
})

需要改为:

$('jquery-selector').each((index, element) => {
    $(element).click();
})

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Filter Bubble

The Filter Bubble

Eli Pariser / Penguin Press / 2011-5-12 / GBP 16.45

In December 2009, Google began customizing its search results for each user. Instead of giving you the most broadly popular result, Google now tries to predict what you are most likely to click on. Ac......一起来看看 《The Filter Bubble》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具