js复制功能

<div class="content"></div>
<el-button @click="copy">复制</el-button>

copy() {
			let pList = document.querySelectorAll(".content")
			let valueList = []
			if (pList.length > 0) {
				pList.forEach(r => {
					valueList.push(r.textContent + '\r\n')
				})
				valueList = [valueList[valueList.length - 1]]
			}
			console.log(valueList)
			let text = valueList.join('')
			let textArea = document.createElement("textarea");
			textArea.value = text
			document.body.appendChild(textArea);
			textArea.select()
			document.execCommand('copy')
			document.body.removeChild(textArea)
			this.$message.success('复制成功!')
		},
posted @ 2024-05-28 09:43  Code_Lzh  阅读(7)  评论(0)    收藏  举报