【问题标题】:React setFocus on Input field - security?反应集专注于输入字段 - 安全性?
【发布时间】:2021-10-24 17:24:34
【问题描述】:

问题:在 React 中,我读到出于安全原因,您不应该使用 'ref' 和 'findDOMNode'。 (不安全地访问本机 DOM 元素...)

还有其他方法可以在输入字段中手动设置焦点吗?

【问题讨论】:

    标签: reactjs security input ref setfocus


    【解决方案1】:

    您可以尝试以下方法:

        function manualFocus() {
        document.getElementById("text").focus();
      }
    
      return (
        <div className="App">
          <input type="text" id="text" name="text"></input>
          <input type="text2" id="text2" name="text2"></input>
          <button type="button" onClick={() => manualFocus()}>Manual Focus</button>
        </div>
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2017-10-13
      • 2019-03-02
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多