简体   繁体   English

如何将隐藏文本区域中的文本复制到剪贴板?

[英]How Can I Copy Text From Hidden Textarea to Clipboard?

I have a hidden text area like this with some values set:我有一个像这样的隐藏文本区域,其中设置了一些值:

<textarea style="display: none;" id="element_html"></textarea>

On click of a button, I try to copy its content to clipboard using this JS code:单击按钮后,我尝试使用以下 JS 代码将其内容复制到剪贴板:

$('#element_html').select();
document.execCommand('copy');

It works only if the text area is visible.仅当文本区域可见时才有效。 How can I copy the hidden text area content to clipboard?如何将隐藏的文本区域内容复制到剪贴板?

opacity: .01;

does the job.做这项工作。 You should combine it with:您应该将其与:

height:0;
position:absolute;
z-index: -1;

Do not use pointer-events:none;不要使用pointer-events:none; as it will stop .select() from working, as well.因为它也会停止.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...">

You can create a temporary input element that is appended to the body, set it its value to the textarea's content and then use it for the copy function.您可以创建一个附加到正文的临时输入元素,将其值设置为 textarea 的内容,然后将其用于复制功能。 Then you remove it from the dom.然后你从dom中删除它。 This will work - irrespective of the display state of the text area.这将起作用 - 无论文本区域的显示状态如何。

Note that I did not create this method - I got it from somewhere (possibly another SO answer) and have used it since in a number of instances.请注意,我没有创建此方法 - 我从某个地方(可能是另一个 SO 答案)获得它,并且在许多情况下都使用过它。

 function copyContents() { var $temp = $("<input>"); var content = $('#element_html').text(); $("body").append($temp); $temp.val(content).select(); document.execCommand("copy"); $temp.remove(); }
 #element_html { display: none }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="element_html">Hidden textarea content</textarea> <button onclick="copyContents()">Click to copy</button> <input type="text" placeholder="Paste here">

The Following codes worked for my problem.以下代码适用于我的问题。 paste the js code inside your script tag/file.also add the css style to hide the textarea.将 js 代码粘贴到您的脚本标签/文件中。同时添加 css 样式以隐藏文本区域。 Also, I found the following ideas through stackoverflow forum so all credit to those folks.此外,我通过 stackoverflow 论坛发现了以下想法,因此所有这些都归功于这些人。

HTML code: HTML代码:

 function cpy(n) { var id=n; var txt=document.getElementById(id); txt.select(); document.execCommand("copy"); };
 /* The Following Code is to Hide textarea from The user view area */ textarea{ opacity: 0; position: absolute; z-index: -9999; pointer-events: none; }
 <!-- readonly attribute is used because i found that in mobile devices, keyboard poped-up while i clicked the button to copy text--> <textarea id="c1" readonly> This is a text from textarea One. </textarea><br> <!--The cpy(this.id) passes button id to js function--> <button id="c1" onclick="cpy(this.id)">Copy</button> <input type=text placeholder="Paste Here to test">

The problem is that since the textarea has its display property set to none , its content can not be selected.问题在于,由于 textarea 的 display 属性设置为none ,因此无法选择其内容。 You can absolutely position the textarea to the left with position: absolute and left: -9999px .您可以使用position: absoluteleft: -9999px将 textarea 绝对定位到左侧。 You should also add z-index: -9999 so it will always be placed under other elements (unless they have a lower z-index).您还应该添加z-index: -9999以便它始终放置在其他元素下(除非它们的 z-index 较低)。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea style="position: absolute; left: -9999px;" id="element_html">Text inside textarea</textarea> <button onClick="copy()"> Copy </button> <p/> <textarea placeholder="Paste text here..."></textarea> <script> function copy(){ $('#element_html').select(); document.execCommand('copy'); } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM