简体   繁体   English

如何在Reactjs中使用类查找所有DOM元素?

[英]How to find all DOM elements with classes in Reactjs?

I'm creating a game right now using React and I need help with finding all elements with a certain className. 我现在正在使用React创建游戏,我需要查找具有特定className的所有元素的帮助。 This is a part of my code: 这是我的代码的一部分:

class FourthLine extends React.Component {
render() {
    const monsterList4 = [];
    for (let i = 0; i < 11; i++) {
        const monsterLine4 = <div className="four-line enemys"></div>
        monsterList4.push(monsterLine4)
    }
    return <div className="line-four">
        {monsterList4}
    </div>
   }
}

I want to get all elements with className "enemys" and read that enemys position x and position y. 我想获取所有具有className“ enemys”的元素,并阅读敌人的位置x和位置y。 Is that even possible using React? 使用React甚至可能吗?

您可以使用document.getElementsByClassName('enemys')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM