我有一个这样的隐藏文本区域,并设置了一些值:
<textarea style="display: none;" id="element_html"></textarea>
单击按钮后,我尝试使用以下JS代码将其内容复制到剪贴板:
$('#element_html').select();
document.execCommand('copy');
它仅在文本区域可见时才起作用。如何将隐藏的文本区域内容复制到剪贴板?
opacity: .01;
做这份工作。您应该结合使用:
height:0;
position:absolute;
z-index: -1;
请勿使用,pointer-events:none;
因为它也会停止.select()
工作。
function copyContents() {
$('#element_html').select();
document.execCommand('copy');
}
#element_html {
position: absolute;
opacity: .01;
height: 0;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="element_html">Which textarea?</textarea>
<button onclick="copyContents()">Copy</button>
<input type="text" placeholder="Paste it here...">
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句