Javascript HTML将光标设置为不同的contenteditable div

Javascript HTML将光标设置为不同的contenteditable div,javascript,html,reactjs,contenteditable,Javascript,Html,Reactjs,Contenteditable,我已经实现了一个项目,其中有一些可编辑的div。当在其中一个div的文本中间按下enter键时,我将div拆分为两个单独的contenteditable div,但当React重新渲染组件时,光标会移到第一个div的开头 我想知道是否有任何方法可以使光标转到第二个div的开头,这将使界面感觉更加自然。如果有帮助的话,我知道两个div的id。下面是一个使用refs聚焦我从react文档中提取的示例 class CustomTextInput extends React.Component {

我已经实现了一个项目,其中有一些可编辑的div。当在其中一个div的文本中间按下enter键时,我将div拆分为两个单独的contenteditable div,但当React重新渲染组件时,光标会移到第一个div的开头


我想知道是否有任何方法可以使光标转到第二个div的开头,这将使界面感觉更加自然。如果有帮助的话,我知道两个div的id。

下面是一个使用
refs
聚焦我从react文档中提取的示例

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    // create a ref to store the textInput DOM element
    this.textInput = React.createRef();
    this.focusTextInput = this.focusTextInput.bind(this);
  }

  focusTextInput() {
    // Explicitly focus the text input using the raw DOM API
    // Note: we're accessing "current" to get the DOM node
    this.textInput.current.focus();
  }

  render() {
    // tell React that we want to associate the <input> ref
    // with the `textInput` that we created in the constructor
    return (
      <div>
        <input
          type="text"
          ref={this.textInput} />
        <input
          type="button"
          value="Focus the text input"
          onClick={this.focusTextInput}
        />
      </div>
    );
  }
}
类CustomTextInput扩展React.Component{
建造师(道具){
超级(道具);
//创建一个ref来存储textInput DOM元素
this.textInput=React.createRef();
this.focusTextInput=this.focusTextInput.bind(this);
}
focusTextInput(){
//使用原始domapi显式聚焦文本输入
//注意:我们正在访问“current”以获取DOM节点
this.textInput.current.focus();
}
render(){
//告诉React我们要关联ref
//使用我们在构造函数中创建的“textInput”
返回(
);
}
}

react中的焦点通常使用
refs
进行处理。好的,我去看看。谢谢@travism这对我有用!事实上,我最终使用了它,只是因为它更容易实现我的项目,因为我知道组件的id<代码>文档.getElementById().focus()