用react做的微信端页面,想达到的效果是在页面加载后input框自动获得焦点能够弹出移动端的键盘,所以在componentDidMount中触发input框的focus,但是无效是怎么回事?
componentDidMount: function () {
var that = this;
React.findDOMNode(this.refs.password).focus();
}
用react做的微信端页面,想达到的效果是在页面加载后input框自动获得焦点能够弹出移动端的键盘,所以在componentDidMount中触发input框的focus,但是无效是怎么回事?
componentDidMount: function () {
var that = this;
React.findDOMNode(this.refs.password).focus();
}
我直接把整段代码搬过来好了:)
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// Explicitly focus the text input using the raw DOM API
this.textInput.focus();
}
render() {
// Use the `ref` callback to store a reference to the text input DOM
// element in an instance field (for example, this.textInput).
return (
<div>
<input
type="text"
ref={(input) => { this.textInput = input; }} />
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
同意楼上的方法:https://reactjs.org/docs/refs...
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答1.9k 阅读✓ 已解决
3 回答1.4k 阅读✓ 已解决
试试:
或
或
參考自: https://facebook.github.io/re...