简体   繁体   English

使用 ref 访问元素内的元素

[英]Access elements inside element with ref

<div ref={location} className={style.locations}>
        {locations.map(loc => (
          <span onClick={onClick}>
            {loc.name}
          </span>
        ))}
</div>

I want to access the <span> inside the <div> with "location" ref:我要访问的<span>里面<div>与“位置”参考:

const span = location.current.getElementsByTagName("span");
span.forEach(el => {
   el.style.display = "none";
})

But I just get error:但我只是得到错误:

span.forEach is not a function

Try this尝试这个

[...span].forEach(el => {
   el.style.display = "none";
})

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

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