![](/img/trans.png)
[英]Getting the name property of the TextField (as a Select Box) Material-UI via the event object from the onFocus prop
[英]How select part of text in a Textfield on onFocus event with material-UI in React?
將setSelectionRange與lastIndexOf
方法結合使用以查找last的位置.
。
class App extends React.Component { handleFocus = event => { event.preventDefault(); const { target } = event; const extensionStarts = target.value.lastIndexOf('.'); target.focus(); target.setSelectionRange(0, extensionStarts); } render(){ return ( <input value={'myfile.doc'} onFocus={this.handleFocus} /> ) } } ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>
我將使用拆分並獲取數組的第一項。
text = "mydoc.doc"; console.log(text.split(".")[0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.