语法
bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
返回值
一个 Boolean
,如果是 false
则表示操作不被支持或未被启用。
微信小程序自带复制功能,那么网页js如何实现复制功能呢?
nput和textarea等文本输入框可以实现选中复制,针对div,p等标签不可以,那么我们需要新建一个文本框标签,不能给display:none; 我们需要怼他进行一个定位,让透明看不见找不到,点击复制,将需要复制的内容赋值给文本框,接着对文本框进行全选,在进行复制操作即可,代码如下:
html部分:
<p id="content">敏感行业列表内涉及到的行业和业务一定无法申请,外显号码申请受政策影响较大</p>
<textarea id="text" style="position: fixed;top: 10000px;left: 10000px;opacity: 0;"></textarea>
<button id="CopyBtn">复制</button>
js部分:
<script>
var content = document.getElementById("content").innerText;
var text = document.getElementById("text");
var CopyBtn = document.getElementById("CopyBtn");
CopyBtn.onclick = function(){
// 将需要复制的内容赋值给文本框
text.value = content;
// 选中文本框的内容
text.select();
// 对选中的内容进行复制
document.execCommand("copy");
}
</script>
点击按钮实现复制文本内容功能
<input class="layui-input" id="copyvalue">
<button type='button' class='layui-btn' onclick=copyfun>点击复制</button>
//复制input框的内容
function copyfun(){
var e = document.getElementById("copyvalue");
e.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
alert("复制成功");
}
进阶:不依靠输入框输入,隐藏input,点击复制自定义的内容
注意:这里input 不能hidden,也不能 display: none,否则复制不成功
<input class="layui-input" id="copyvalue" style="opacity: 0;position: absolute;">
<button type='button' class='layui-btn' onclick=copyfun>点击复制</button>
//复制input框的内容
function copyfun(){
let text='要赋值的内容'
$('#copyvalue').val(text)
var e = document.getElementById("copyvalue");
e.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
alert("复制成功");
}
改进方法
这样可以不用在html里写 input,灵感来源于 创建 a 标签下载链接
function copyfun(){
var copyipt = document.createElement("input");
var text = "需要复制的内容";
copyipt.setAttribute("value", text);
document.body.appendChild(copyipt);
copyipt.select();
document.execCommand("copy");
document.body.removeChild(copyipt);
}
在PC端上通过点击按钮复制对应文本的业务场景,不经常有,但是让我碰见了,也了解了下实现方法。
1、通过createElement生成一个input输入框,并将其透明度设为0,使得用户无感知;
2、将需要复制的内容赋值到输入框内,(一定要将输入框添加到页面body中)才能使用select函数选中。
3、通过document.execCommand(“copy”)执行浏览器复制命令,就能将对应文本进行复制了
function handleCopy(text) {
const input = document.createElement('input')
input.style.cssText = 'opacity: 0;';
input.type = 'text';
input.value = text; // 修改文本框的内容
document.body.appendChild(input)
input.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
this.$message({message: '复制成功',type: 'success'})
},
select()
方法用于选择该元素中的文本。document.execCommand('copy')
执行浏览器复制命令bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
一个 Boolean
,如果是 false
则表示操作不被支持或未被启用。
注意:在调用一个命令前,不要尝试使用返回值去校验浏览器的兼容性
backColor
<color>
类型的字符串值作为参数传入。注意,IE浏览器用这个设置文字的背景颜色。
bold
<strong>
标签,而不是
<b>
标签。
ClearAuthenticationCache
contentReadOnly
copy
createLink
href
URI字符串作为参数值传入。URI必须包含至少一个字符,例如一个空格。(浏览器会创建一个空链接)
cut
decreaseFontSize
<small>
标签,或在选中点插入该标签。(IE浏览器不支持)
defaultParagraphSeparator
delete
enableAbsolutePositionEditor
enableInlineTableEditing
enableObjectResizing
fontName
fontSize
foreColor
formatBlock
forwardDelete
heading
hiliteColor
increaseFontSize
indent
insertBrOnReturn
insertHorizontalRule
insertHTML
insertImage
insertOrderedList
insertUnorderedList
insertParagraph
insertText
italic
justifyCenter
justifyFull
justifyLeft
justifyRight
outdent
paste
redo
removeFormat
selectAll
strikeThrough
subscript
superscript
underline
undo
unlink
useCSS
styleWithCSS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style type="text/css">
.wrapper {
position: relative;
}
#input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
z-index: -10;
}
</style>
</head>
<body>
<div class="wrapper">
<p id="text" onclick="copyText()">复制的一段文字</p>
<input id="input" value="躲起来的文本框"/>
<!-- <textarea id="input">也可以使用文本框</textarea> -->
<button onclick="copyText()">copy</button>
</div>
<script type="text/javascript">
function copyText() {
var text = document.getElementById("text").innerText;
var input = document.getElementById("input");
input.value = text; // 修改文本框的内容
input.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
alert("复制成功");
}
</script>
</body>
</html>