js複製功能

Code_Lzh發表於2024-05-28
<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('複製成功!')
		},

相關文章