BING
Surmon's digital garden
OG

极简的剪切板组件

716 characters, 2 min read2018/01/05 PM5,386 views

不再用 clipboard.js 了,所以:

          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
<!-- 这就是剪贴板载体,想办法视觉隐藏就可以 --> <input type="text" v-model="clipboardText" class="clipboard-input" ref="clipboard"> <button @click="copyToClipboard('123')"></button> <button @click="copyToClipboard('abc')"></button> <button @click="copyToClipboard('789')"></button>
          
  • 1
  • 2
  • 3
  • 4
.clipboard-input { height: 0px; opacity: 0; }
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
export default { data() { return { clipboardText: '' } }, methods: { copyToClipboard(text) { console.log('要复制这个文本:', text) this.clipboardText = text // $nextTick 在这里没用 setTimeout(() => { this.$refs.clipboard.select() document.execCommand('Copy') }) } } }

好,完

Creative Commons BY-NC 4.0 https://surmon.me/article/80
8 / 8 comments
Guest
Join the discussion...
  • Superficial
    Superficial🇨🇳CNWuhanWindowsChrome

    大佬, 太强大了, 👍

  • channg
    Channg🇨🇳CNBeijingWindowsChrome
  • Zzhiren
    Zzhiren🇨🇳CNHangzhouWindowsChrome

    666666

  • 11
    11🇨🇳CNXinminWindowsChrome

    11

    • 121
      121🇨🇳CNXinminWindowsChrome

      reply:

      回复1

  • Yunzhuiyue
    Yunzhuiyue🇨🇳CNQingdaoWindowsChrome
              
    • 1
    alert('NB');
    • Surmon
      Surmon🇨🇳CNXiamenMac OSChrome

      reply:

      爱你

  • Jkchao
    Jkchao🇹🇼TWPingzhenMac OSChrome
              
    • 1
    • 2
    console.log('666')