jquery - 在react componentDidMount中bind(this)后获取jquery对象

标签 jquery reactjs

在我的应用程序中,我在 componentDidMound 中使用第 3 方插件,如下所示:

clickStuff: function () {
    //do stuff
},
componentDidMount: function () {

    $(".whatever").on { 'click', function() {
         this.clickStuff();
     }.bind(this));
 }

这使得如果我单击任何具有“whatever”类的 div 即可转到 react 函数“clickStuff()”;

但是如果我想获取“这个”jquery 节点怎么办?喜欢 $(".whatever") 的属性吗?例如:

componentDidMount: function () {

    $(".whatever").on { 'click', function() {
         //this will cause conflict between the two this
         $(this).attr("title", this.state.titleMessage);
     }.bind(this));
 }

$(this)中的this应该是jquery节点,“this.state.titleMessage”中的this应该是react部分。

如何让两者成为同一个函数的一部分?或者我需要做一些奇特的事情吗?我对如何做到这一点有点困惑。我不知道该怎么称呼这个问题。

最佳答案

旧方法:

componentDidMount: function () {
    var that = this;
    $(".whatever").on { 'click', function() {
        //that is the this of componentDidMount (and so of clickStuff)
        that.clickStuff();
        //this remains the same
        $(this).attr("title", that.state.titleMessage);
    });
}

关于jquery - 在react componentDidMount中bind(this)后获取jquery对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35352344/

相关文章:

javascript - 如何在JavaScript中转换日期格式?

javascript - 无法在未登录的情况下重定向到重置密码链接

javascript - 数据库插入后刷新 View React Redux

javascript - Angular 和 Vue 模板和绑定(bind)

javascript - 支持选中所选行的复选框?

jquery - 如何在图像上覆盖透明 PNG/在其他元素上滚动时/

javascript - 在移动设备上禁用视差

javascript - 将 Javascript 添加到自定义小部件

javascript - 如何使 onClick 和 href 一起工作?

javascript - 检查 DraftJS 中的 contentState 是否更改或只是 editorState 的最佳性能方法