鍍金池/ 問(wèn)答/HTML5  HTML/ select()也有底線?

select()也有底線?

document.execCommand

當(dāng)一個(gè)HTML文檔切換到設(shè)計(jì)模式(designMode)時(shí),文檔對(duì)象暴露 execCommand方法,該方法允許運(yùn)行命令來(lái)操縱可編輯區(qū)域的內(nèi)容。大多數(shù)命令影響文檔的選擇(粗體,斜體等),而其他命令插入新元素(添加鏈接)或影響整行(縮進(jìn))。當(dāng)使用 contentEditable時(shí),調(diào)用 execCommand() 將影響當(dāng)前活動(dòng)的可編輯元素。

相關(guān)的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>

如上所示代碼,當(dāng)寬度小于5px時(shí),復(fù)制就沒(méi)有效果。

這是什么坑?

回答
編輯回答
亮瞎她

我用火狐,需要使用 document.execCommand('copy' , false)。寬度倒是沒(méi)影響。

2018年8月7日 20:40