如何使用JQuery打字稿中的每个函数 [英] How use JQuery each function in typescript

查看:86
本文介绍了如何使用JQuery打字稿中的每个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取每个复选框的类/属性.代码示例如下.

I want to get class/attr of each checkbox. Code sample is given below.

  jQuery("input[type='checkbox']").each(()=> {
      let checkboxID = jQuery(this).attr("class");
      console.log(checkboxID);//output undefined
      console.log(this.atc1List); //typescript variable 
    });

推荐答案

内部箭头函数this引用您的类实例,因此,请按如下所示更新代码,其中callback中的第二个参数引用该元素.

Inside arrow function this refers to your class instance so update your code as follows, where the second argument in callback refers to the element.

jQuery("input[type='checkbox']").each((i, ele) => {
  let checkboxID = jQuery(ele).attr("class");
  console.log(checkboxID);//output undefined
  console.log(this.atc1List); //typescript variable 
});

根据 MDN文档:

箭头函数表达式的语法比函数表达式短,并且没有自己的 this ,arguments,super或new.target.

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.

这篇关于如何使用JQuery打字稿中的每个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆