gpt4 book ai didi

javascript - 从 JQ 函数调用 react 函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:12 24 4
gpt4 key购买 nike

我需要从 JQ 函数调用 React 函数 (this.clearMath())

$('.input-content').focus(
function(){
this.clearMath()
})

我收到未捕获的类型错误:this.clearMath 不是一个函数。我认为这是由 JQ 引起的,它认为 this. 是对所选元素 $('.input-content') 的引用。

我说的对吗?以及如何区分 react this 和 jquery this 才能调用我的函数?谢谢

最佳答案

你可以通过这样做来解决这个问题:

var _ = this;
$('.input-content').focus(
function(){
//this is still the input
_.clearMath()
})

因此,您将 this 上下文保存在选择器之前,以便您可以访问函数内部的 _,它称为 closure .

关于javascript - 从 JQ 函数调用 react 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42275776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com