input 獲取焦點時自動全選里面的文字


<input type="text" id="demo">
// 獲取焦點,選中里面的文字
// 1.獲取input 框的元素
      var input = document.getElementById('demo')
// 2.獲取input 框的值
      var value = input.innerText
// 3.通過setSelectionRange 選中文字
      input.setSelectionRange(0, value.length)

在vue通過ref拿不到setSelectionRange方法,可能需要通過id主動獲取,並且可能需要添加異步
    const input = document.getElementById(`input`);
      input.focus()
      setTimeout(()=>{ // 或者使用nextTick
        inputList.setSelectionRange(0,this.searchLetter.length);
      })


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM