You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中getAttribute函数无法稳定获取SVG元素name属性值问题

问题:交替点击SVG按钮后,event.target.getAttribute('name')返回null
  • 我制作了测试示例,交替点击“first”和“back”按钮多次后,控制台显示事件目标的name属性变为null,但首次点击时该属性不为空。
  • 附上带注释的截图说明该现象:带注释的现象截图

相关代码如下:

handleSearchChange(event) {
  const target = event.target;
  const name = target.getAttribute("name");
  console.log("Test name " + name + "\n");
}
render() {
  return (
    <div>
      <div style={{ height: "30px", width: "30px" }}>
        <FirstSVG name="first_page" onClick={this.handleSearchChange} />
      </div>
      <div style={{ height: "30px", width: "30px" }}>
        <BackSVG name="back_page" onClick={this.handleSearchChange} />
      </div>
    </div>
  );
}

内容的提问来源于stack exchange,提问作者Victorqedu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:15:44