document.execCommand
当一个HTML文档切换到设计模式(designMode)时,文档对象暴露 execCommand方法,该方法允许运行命令来操纵可编辑区域的内容。大多数命令影响文档的选择(粗体,斜体等),而其他命令插入新元素(添加链接)或影响整行(缩进)。当使用 contentEditable时,调用 execCommand() 将影响当前活动的可编辑元素。
相关的API可以参考MDN
<html>
<head>
</head>
<body>
<form>
<input type="text" id="myText" value="A cat played with a ball" style="width:5px">
<input type="button" value="Select text" onclick="selText()">
</form>
<script type="text/javascript">
function selText() {
document.getElementById("myText").select();
document.execCommand('copy' , true);
}
</script>
</body>
</html>
如上所示代码,当宽度小于5px时,复制就没有效果。
这是什么坑?
我用火狐,需要使用
document.execCommand('copy' , false)
。宽度倒是没影响。