最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call react function from JQ function - Stack Overflow

programmeradmin4浏览0评论

I need to call React function (this.clearMath()) from JQ function

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

I got Uncaught TypeError: this.clearMath is not a function. I think it´s caused by JQ that thinks this. is reference to selected element $('.input-content').

Am I right? And how to distinguish between react this an jquery this to be able call my function? Thanks

I need to call React function (this.clearMath()) from JQ function

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

I got Uncaught TypeError: this.clearMath is not a function. I think it´s caused by JQ that thinks this. is reference to selected element $('.input-content').

Am I right? And how to distinguish between react this an jquery this to be able call my function? Thanks

Share Improve this question asked Feb 16, 2017 at 13:48 M. FrydlM. Frydl 731 silver badge9 bronze badges 2
  • this makes reference to the scope where you currently are. In this case, you are right, the scope is of the selector. I'm not sure why are you trying to call a React function from jQuery. That doesn't seems right. As far as I'm concerned, it is not possible to call a React function defined inside a React ponent from jQuery. – juanecabellob Commented Feb 16, 2017 at 13:56
  • Can you post your code in a jsfiddle for better understanding what you are trying to do? – juanecabellob Commented Feb 16, 2017 at 13:59
Add a ment  | 

1 Answer 1

Reset to default 8

You can solve this by doing this:

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

So you save the this context before the selector so you can access the _ inside the function, its called a closure.

发布评论

评论列表(0)

  1. 暂无评论