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