How can I simulate a different button pressed when a button is pressed?

后端 未结 2 1852
予麋鹿
予麋鹿 2021-01-14 19:30

Is it possible using vue.js to simulate another button pressed when a button is pressed?

For example, if I press the (Arrow down) button, I would l

相关标签:
2条回答
  • 2021-01-14 20:11

    Sure, add a ref to tab

    <div class="tab" ref="tab">
    

    Then capture arrow click:

    arrowClick() {
       this.$refs.tab.click()
    }
    
    0 讨论(0)
  • 2021-01-14 20:20

    I think you're asking how to simulate a TAB keypress when the user presses DOWN with the goal of moving the focus to the next focusable input.

    Instead of rewriting the key-event, you could call HTMLElement#focus() on the next sibling:

    <!-- TEMPLATE -->
    <input type="text"
        @keydown.up.stop.prevent="prevInput"
        @keydown.down.stop.prevent="nextInput"
        v-for="i in 5">
    
    // SCRIPT
    methods: {
      nextInput(e) {
        const next = e.currentTarget.nextElementSibling;
        if (next) {
          next.focus();
        }
      },
      prevInput(e) {
        const prev = e.currentTarget.previousElementSibling;
        if (prev) {
          prev.focus();
        }
      },
    }
    

    <script src="https://unpkg.com/vue@2.5.17"></script>
    
    <div id="app">
      <input type="text"
             @keydown.up.stop.prevent="prevInput"
             @keydown.down.stop.prevent="nextInput"
             v-for="i in 5">
    </div>
    
    <script>
    new Vue({
      el: '#app',
      methods: {
        nextInput(e) {
          const next = e.currentTarget.nextElementSibling;
          if (next) {
            next.focus();
          }
        },
        prevInput(e) {
          const prev = e.currentTarget.previousElementSibling;
          if (prev) {
            prev.focus();
          }
        },
      }
    })
    </script>

    0 讨论(0)
提交回复
热议问题