简体   繁体   English

将 textarea 文本复制到剪贴板 html / javascript

[英]Copy textarea text to clipboard html / javascript

hey, i know there's lots of tutorials out there but none seem to be working for me.嘿,我知道那里有很多教程,但似乎没有一个对我有用。

I have this :我有这个:

<textarea name="forum_link" type="text" style="width:630px; height:90px;">
[URL=http://www.site.net/video/<?=$_GET['id']?>/<?=$_GET['tag']?>]<?=$video->title?>[/URL]

[URL=http://www.site.net/video/<?=$_GET['id']?>/<?=$_GET['tag']?>][IMG]<?=$video->thumbnailURL?>[/IMG][/URL]
</textarea>

Now all i want is a simple button, that when clicked copies the text in the textarea to the users clipboard.现在我想要的只是一个简单的按钮,单击该按钮时会将 textarea 中的文本复制到用户剪贴板。

Any help would be great :)任何帮助都会很棒:)

Thanks谢谢

<textarea id="html" name="html">Some text</textarea>
<input type="button" value="Refresh" onclick="copy_to_clipboard('html');">

<script>
function copy_to_clipboard(id)
{
    document.getElementById(id).select();
    document.execCommand('copy');
}
</script>

Check out this page.看看这个页面。 It doesn't say anything about browser compatibility, but could be worth checking out!它没有说明浏览器兼容性,但值得一试! It gives a javascript copy to clipboard example, and the HTML associated with it.它为剪贴板示例提供了一个 javascript 副本,以及与之关联的 HTML。

http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html http://www.geekpedia.com/tutorial126_Clipboard-cut-copy-and-paste-with-JavaScript.html

Sadly there's no all in one solution for this.遗憾的是,对此没有一劳永逸的解决方案。 Browsers other than IE doesnt allow copying to clipboard. IE 以外的浏览器不允许复制到剪贴板。 I found I nice solution recently which uses Flash (for all browsers but IE) and JavaScript for IE to copy text to the clipboard.我最近发现了一个很好的解决方案,它使用 Flash(适用于除 IE 之外的所有浏览器)和 JavaScript for IE 将文本复制到剪贴板。 See zeroclipboard for details.有关详细信息,请参阅zeroclipboard

Unfortunately javascript does not have a universal way.不幸的是,javascript 没有通用的方式。 Currently, the use of flash & javascript most universal way.目前,使用flash & javascript 最普遍的方式。 Look on a LMCButton - a small animated flash button (4 kb) for "Copy to clipboard".查看LMCButton - 一个用于“复制到剪贴板”的小型动画 Flash 按钮 (4 kb)。

Example of using javascript:使用 javascript 的示例:

Get html code of the button: function lmc_get_button(cliptext, idLMC)获取按钮的html代码: function lmc_get_button(cliptext, idLMC)

Update text in the button: function lmc_set_text(idLMC, text)更新按钮中的文本: function lmc_set_text(idLMC, text)

The solution is purely on Javascript.解决方案纯粹是基于 Javascript。 i don't know about its compatibility with other browsers.我不知道它与其他浏览器的兼容性。 For chrome its working, I am adding the code snippet.对于 chrome 的工作,我添加了代码片段。

 //all text written(inside text area), is copied and shown inside the div with class "mas" //you can't see it, as it is hidden(opacity is 0) $('#content:not(.focus)').keyup(function(){ var value = $(this).val(); var contentAttr = $(this).attr('name'); $('.'+contentAttr+'').html(value.replace(/\\r?\\n/g,'<br/>')); }) //below code can copy text inside a div. div id should be identical with button oclick id copyToClipboard = function (element) { var $temp = $("<input />"); $("body").append($temp); $temp.val($(element).text()).select(); var result = false; try { result = document.execCommand("copy"); } catch (err) { console.log("Copy error: " + err); } $temp.remove(); return result; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <textarea name="mas" rows="6" id="content"></textarea> <p>&nbsp;</p> <div id="p1" class="mas" style="top:0px;position:absolute;opacity:0;" ></div> <button onclick="copyToClipboard('#p1')">Copy P1</button>

Please see this Jsfiddle for more detail.有关更多详细信息,请参阅此Jsfiddle

Browser compatibility using any script is shoddy at best.使用任何脚本的浏览器兼容性充其量都是劣质的。 JavaScript intentionally doesn't natively allow this level of functionality with the operating system. JavaScript 故意不允许在操作系统中使用这种级别的功能。 It is possible to create a signed script that you'll have better luck with, but... that's a lot more work and hardly worth it.可以创建一个签名的脚本,这样你的运气会更好,但是......这需要做更多的工作,而且几乎不值得。 Most people know how to copy and paste...大多数人都知道如何复制和粘贴...

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

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