select()也有底线?

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时,复制就没有效果。

这是什么坑?

阅读 1.5k
1 个回答

我用火狐,需要使用 document.execCommand('copy' , false)。宽度倒是没影响。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题