在我的应用程序中,我在 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/