我尝试使用ref进行getElementByClassName,但是错误是getElementByClassName不是函数,我不能对指定的元素使用ref。有人能帮忙吗?
const ref = useRef(null);
//...
const handleAction = () => {
if(ref.current) {
console.log('I get here')
const elem = ref.current.getElementByClassName('myClass')
}
}
return (<div>
<component ref={ref} />
<button onClick={handleAction} />
</div>)我到了console.log(“我来了”)
发布于 2021-06-04 06:49:34
将语句更改为
const elem = ref.current.getElementsByClassName('myClass')函数名包括elements (复数),而不是element。
此外,它还返回所有匹配元素的数组。
https://stackoverflow.com/questions/67832599
复制相似问题