gpt4 book ai didi

javascript - React - 未捕获类型错误 : this. getDOMNode 不是函数

转载 作者:行者123 更新时间:2023-12-02 15:01:00 24 4
gpt4 key购买 nike

我试图通过 react 组件中的jquery应用样式,但是我收到错误Uncaught TypeError: this.getDOMNode is not a function

topicsVisited(arr){
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(this.getDOMNode()).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({
'background-color': 'red'
});
});
});
};

最佳答案

您需要绑定(bind)函数才能正确使用this

topicsVisited(arr) {
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(this.getDOMNode()).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({'background-color': 'red'});
}.bind(this));
}.bind(this);
}

或创建一个引用正确this的变量。

topicsVisited(arr) {
var self = this;
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(self.getDOMNode()).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({'background-color': 'red'});
});
};
}

关于javascript - React - 未捕获类型错误 : this. getDOMNode 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430377/

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