【问题标题】:Onblur is automatically called on OnFocus event in ReactOnblur 在 React 中的 OnFocus 事件上自动调用
【发布时间】:2018-05-22 10:21:44
【问题描述】:

我正在做出反应。我想在单击按钮时显示文本框,并尝试显示有关聚焦文本框的建议。所以我尝试了OnFocus() 事件,当我们点击文本框时我想隐藏建议,所以我使用了onBlur() 事件。当我手动聚焦文本框时,它工作正常。

当我尝试在单击按钮时自动聚焦时,也会触发 onBlur() 并隐藏建议。我不知道为什么。帮我解决这个问题。

代码:

<input type="text" tabIndex="1"   onFocus={//focus handler//} placeholder="Enter Search value" ref="search_txt"  onBlur={this.onBlurEvent} />

componentDidMount(){
       this.refs.search_txt.focus();
}

【问题讨论】:

  • 能否提供完整代码,对您有帮助

标签: reactjs


【解决方案1】:

您可以参考以下链接

https://coderwall.com/p/0iz_zq/how-to-put-focus-at-the-end-of-an-input-with-react-js

moveCaretAtEnd(e) {
  var temp_value = e.target.value
  e.target.value = ''
  e.target.value = temp_value
}


render() {
  <textarea 
    value={value}
    autoFocus
    onFocus={this.moveCaretAtEnd}
  ></textarea>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多