我正在嘗試使用本機 JS 將參數(shù)字符串復制到剪貼板中。到目前為止,這工作正常,但是在 IE 7 中運行我的代碼段時,我有一個小的外觀問題。我的代碼:function copyStringToClipboard (str) { // Create new element var el = document.createElement('input'); el.setAttribute("display", "none"); el.setAttribute("type", "text"); el.value = str; el.setAttribute('readonly', ''); document.body.appendChild(el); el.select(); // Copy text to clipboard document.execCommand('copy'); // Remove temporary element document.body.removeChild(el);}正如我上面提到的,這在經(jīng)過測試的瀏覽器中確實有效。但是,它創(chuàng)建了一個可見的文本輸入字段(第 3 行)。我嘗試使用el.style = {position: 'absolute', left: '-9999px'};,但 Internet Explorer 產(chǎn)生:未實現(xiàn)我想過創(chuàng)建一個input type="hidden",但似乎這個隱藏字段是不可選擇的 - 這是有道理的。不用說,這個動作會觸發(fā)onClick(),所以確實是一個用戶動作。關(guān)于如何解決這個問題的想法?
將字符串復制到帶有隱藏字段的剪貼板
喵喵時光機
2021-11-04 17:47:45