HTMLCollection []未定义

时间:2018-09-19 18:52:59

标签: javascript reactjs typescript

rowGetter = i => {
    const row = this.state.estimateItemList[i];
    const selectRevison = this.state.selectedEstimate.revision;
    const rowLenght = this.state.estimateItemList.length;
    const changeColor = document.getElementsByClassName('rd') as 
    HTMLCollectionOf<HTMLElement>;
    if (row.revision > selectRevison) {
        for (let x = 0; x < changeColor.length; x++) {
            row.changeColor.style.backgroundColor = 'red';
            // changeColor.style.backgroundColor = 'red'; //this is get error(Cannot read property 'style' of undefined)
        }
        return row;
    }
}

我想在条件row.revision > this.state.selectedEstimate.revision下更改行颜色。如何防止更改this.color。但是,我没有收到任何错误,但是行颜色没有更改,并且changeColor : HTMLCollection []没有得到任何项目。它始终是undefined。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

可以请尝试一下:

const changeColor = document.getElementsByClassName('rd');

而不是:

const changeColor = document.getElementsByClassName('rd')为     HTMLCollectionOf;