我正在尝试使用本机 JS 将参数字符串复制到剪贴板中。到目前为止,这工作正常,但是在 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);}正如我上面提到的,这在经过测试的浏览器中确实有效。但是,它创建了一个可见的文本输入字段(第 3 行)。我尝试使用el.style = {position: 'absolute', left: '-9999px'};,但 Internet Explorer 产生:未实现我想过创建一个input type="hidden",但似乎这个隐藏字段是不可选择的 - 这是有道理的。不用说,这个动作会触发onClick(),所以确实是一个用户动作。关于如何解决这个问题的想法?
1 回答
![?](http://img1.sycdn.imooc.com/533e4d5b0001d57502200203-100-100.jpg)
长风秋雁
TA贡献1757条经验 获得超7个赞
而不是使用的el.setAttribute("display", "none");
,你应该改变该行:
el.style.display = "none";
为什么这有效? 设置属性 display none 不影响样式。它应该添加为内联样式或在 css 中隐藏输入框。
添加回答
举报
0/150
提交
取消