業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
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?