欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

复制内容到剪切板

程序员文章站 2022-05-16 11:22:01
...
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<p><span ondblclick="copy(this)" style="" >[email protected].bm</span></p>
	</body>
	<script>
		function copy(that) {
			that.style.background = '#efe78280';
			var text = that.innerText;
		    var textareaEl = document.createElement('textarea');
		    textareaEl.setAttribute('readonly', 'readonly'); // 防止手机上弹出软键盘
		    textareaEl.value = text;
		    document.body.appendChild(textareaEl);
		    textareaEl.select();
		    var res = document.execCommand('copy');
		    document.body.removeChild(textareaEl);
		    setTimeout(function(){
		    	alert("复制成功!");
		        that.style.background = '';
		    },800)
		    return res;
		}
	</script>
</html>

参考链接:https://www.cnblogs.com/zhaodesheng/p/11464934.html

相关标签: javascript