How can I use the event @select to select some text from a paragraph in Vue.js?

前端 未结 2 834
深忆病人
深忆病人 2021-01-28 06:10

I\'m using Vue.js with Nuxt in SSR, I would like when I highlight some text to get this text and perform an action on it.

I found a way to do that with

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 07:04

    I'm unclear on whether you want to react to a selection inside the input or elsewhere in the document. Assuming inside the input:

    • Listen to mouseup event.
    • Pass the event itself with $event.
    • getSelection() doesn't work on input values, so use selectionStart and selectionEnd to get the selection.

    new Vue({
      el: '#app',
      methods: {
        logSelectionWithinInput(e) {
          var selection = e.target.value.substring(
            e.target.selectionStart,
            e.target.selectionEnd
          );
          console.log(selection);
        }
      }
    });
    
    

提交回复
热议问题