简体   繁体   English

不能使用$('。foo')。on('click','。bar',使用jQuery和TypeScript

[英]Can't use $('.foo').on('click', '.bar', with jQuery and TypeScript

I am using TypeScript and jQuery in a project and I need to use jQuery's on to listen to an event: 我使用的打字稿和jQuery在一个项目中,我需要使用jQuery的on听的事件:

const $previewListWrp: jQuery<HTMLElement> = $('.js-preview-list-wrp').first();

$previewListWrp.on('click', '.btn-action.remove', (evt: Event): void => {
  let $box: JQuery<HTMLElement> = $(evt.target).closest('.js-preview-wrp');
  console.log($box.data());
});

However, I get the error 但是,我得到了错误

Argument of type '"click"' is not assignable to parameter of type 'PlainObject void> | “click”类型的参数不能分配给'PlainObject void> |类型的参数 EventHandlerBase EventHandlerBase

Just started a new project and installed the latest versions of TypeScript, jQuery and @types/jQuery. 刚刚开始一个新项目,并安装了最新版本的TypeScript,jQuery和@ types / jQuery。

在此输入图像描述

尝试将evt:EventTarget更改为evt:JQuery.Event

You could use the alternative signature of the on method, in which the first argument is indeed an object, and do: 您可以使用on方法的替代签名,其中第一个参数确实是一个对象,并执行:

$previewListWrp.on({
    'click': (evt: EventTarget): void => {
        console.log(evt);
    }
}, '.btn-action.remove');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM