日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

React - 當(dāng)輸入框獲取焦點(diǎn)時(shí)自動(dòng)選中輸入框中的內(nèi)容

作者:WHOVENLY 更新時(shí)間: 2022-10-11 編程語言

在工作中遇到一個(gè)當(dāng)輸入框獲取焦點(diǎn)時(shí)自動(dòng)選中輸入框中的內(nèi)容這么一個(gè)小功能,這里記錄下,實(shí)現(xiàn)其實(shí)很簡(jiǎn)單,如下所示.


import React from "react";
// 當(dāng)聚焦到A輸入框后,選中該輸入框中的值
export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.txt = {
      curren: null,
    };
  }
  // 當(dāng)A輸入框聚焦后觸發(fā)
  focus_handler = (e) => {
    // 獲取輸入框中的值
    const value = e.target.value;
    // 設(shè)置選中輸入框中的全部文本
    this.txt.setSelectionRange(0, value.length);
  };
  getARef = (el) => {
    this.txt = el;
  };
  render() {
    return (
      <div>
        <input
          type="text"
          placeholder="A輸入框"
          ref={this.getARef}
          onFocus={this.focus_handler}
        />
      </div>
    );
  }
}

原文鏈接:https://blog.csdn.net/huanan__/article/details/126997344

欄目分類
最近更新