17
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vuejsで子コンポーネントの表示内容をクリップボードにコピー

Posted at

Javascriptはあまり詳しくないAndroidエンジニアが、Nuxt.jsとVuetifyでWebページを作成している。

やりたいこと

HTMLで表示した内容をクリップボードにコピーしたい
datapropsで渡した値ではない)

できたコード

これでいけた
div内の全pタグのテキストがクリップボードにコピーできた。

<template>
  <v-layout>
    <v-btn
      icon
      fab
      color="orange"
      @click="witeToClipboard()"
    >
      <v-icon>file_copy</v-icon>
    </v-btn>

    <div id="target">
      <p>コピーしたい文章1</p>
      <p>コピーしたい文章2</p>
      <p>コピーしたい文章3</p>
    </div>
  </v-layout>
</template>

<script>
export default {
  methods: {
    witeToClipboard() {
      const copyText = this.$el.querySelector('#target').textContent
      navigator.clipboard
        .writeText(copyText)
        .then(() => {
          console.log('テキストコピー完了')
        })
        .catch(e => {
          console.error(e)
        })
    }
  }
}
</script>

参考ページ

17
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?