【问题标题】:Access elements inside element with ref使用 ref 访问元素内的元素
【发布时间】:2020-04-02 13:30:36
【问题描述】:
<div ref={location} className={style.locations}>
        {locations.map(loc => (
          <span onClick={onClick}>
            {loc.name}
          </span>
        ))}
</div>

我想使用“位置”参考访问&lt;div&gt; 内的&lt;span&gt;

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

但我只是得到错误:

span.forEach is not a function

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多