javascript - How to trigger the input focus event inside a method in Vue.js? - Stack Overflow

I have an input for which is using the following event :<b-nputclass="input"id="link&

I have an input for which is using the following event :

 <b-nput
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
            @focus="$event.target.select()"
          ></b-input>

How can I use this @focus="$event.target.select()" event inside:

The above method copies the value. I need to trigger the above select focus event when the user clicks copy How can be it achieved correctly?

I have an input for which is using the following event :

 <b-nput
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
            @focus="$event.target.select()"
          ></b-input>

How can I use this @focus="$event.target.select()" event inside:

The above method copies the value. I need to trigger the above select focus event when the user clicks copy How can be it achieved correctly?

Share Improve this question edited Aug 17, 2020 at 12:03 asked Aug 17, 2020 at 9:27 user12009061user12009061
Add a ment  | 

3 Answers 3

Reset to default 2

Add saved method as focus event handler :

@focus="saved"

method :

methods: {
  saved(event){ //the event is passed automatically as parameter
     event.target.select()
  }
}

Edit :

Try to add ref to the input element

 <b-input
          ref="input"
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
         
            @focus="$event.target.select()"
          ></b-input>

then inside the method trigger the focus programmatically :

 methods: {
      async copy(s) {
      await navigator.clipboard.writeText(s) 
      this.$refs.input.focus();
     ...
    }
}  

Give a ref to your input :

<b-input
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
            ref="theInput"
          ></b-input>

then anywhere in your Component script :

this.$refs['theInput'].focus();

You can have the $event reference to the saved method

  <b-nput
        class="input"
        id="link"
        v-model="link"
        placeholder="link"
        @focus="saved"
      ></b-input>


methods: {
  saved(event){
    console.log(event)
  }
}

ref - https://v2.vuejs/v2/guide/events.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308721a4567845.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信