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

发布于 2024-09-24 02:34:28 字数 493 浏览 3 评论 0 原文

嘿,我知道那里有很多教程,但似乎没有一个适合我。

我有这个:

<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>

现在我想要的只是一个简单的按钮,单击该按钮时会将文本区域中的文本复制到用户剪贴板。

任何帮助都会很棒:)

谢谢

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.

Any help would be great :)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

仅一夜美梦 2024-10-01 02:34:28
<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>
<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>
墨洒年华 2024-10-01 02:34:28

遗憾的是,对此没有一劳永逸的解决方案。 IE 以外的浏览器不允许复制到剪贴板。我最近发现了一个很好的解决方案,它使用 Flash(适用于除 IE 之外的所有浏览器)和适用于 IE 的 JavaScript 将文本复制到剪贴板。有关详细信息,请参阅 zeroclipboard

Sadly there's no all in one solution for this. Browsers other than IE doesnt allow copying to clipboard. I found I nice solution recently which uses Flash (for all browsers but IE) and JavaScript for IE to copy text to the clipboard. See zeroclipboard for details.

女皇必胜 2024-10-01 02:34:28

查看此页面。它没有说明任何有关浏览器兼容性的信息,但可能值得一看!它提供了一个 javascript 复制到剪贴板的示例,以及与其关联的 HTML。

http://www.geekpedia.com/tutorial126_Clipboard-使用 JavaScript 剪切复制粘贴.html

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.

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

紙鸢 2024-10-01 02:34:28

该解决方案纯粹基于 Javascript。我不知道它与其他浏览器的兼容性。对于 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> </p>
<div id="p1" class="mas" style="top:0px;position:absolute;opacity:0;" ></div>

<button onclick="copyToClipboard('#p1')">Copy P1</button>

请参阅此 Jsfiddle 了解更多详细信息。

The solution is purely on Javascript. i don't know about its compatibility with other browsers. For chrome its working, I am adding the code snippet.

//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> </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.

給妳壹絲溫柔 2024-10-01 02:34:28

现代解决方案

document.execCommand('copy') 现在是 已弃用

相反,我们现在有 Clipboard API

您可以使用 writeText() 属性来完成此操作:

$('button').on('click', () => {
  navigator.clipboard.writeText($('textarea').val()).then(
    () => {
      console.log('clipboard successfully set');
    },
    () => {
      console.error('clipboard write failed');
    }
  );
});

或者只是简单地:

$('button').on('click', () => {
  navigator.clipboard.writeText($('textarea').val());
});

奖励:这适用于禁用的文本区域,并且跨浏览器兼容

Modern Solution

document.execCommand('copy') is now deprecated

Instead, we now have the Clipboard API

You can use the writeText() property to accomplish this:

$('button').on('click', () => {
  navigator.clipboard.writeText($('textarea').val()).then(
    () => {
      console.log('clipboard successfully set');
    },
    () => {
      console.error('clipboard write failed');
    }
  );
});

or just simply:

$('button').on('click', () => {
  navigator.clipboard.writeText($('textarea').val());
});

Bonus: This works with disabled textareas and is cross-browser compatible

黎歌 2024-10-01 02:34:28

使用任何脚本的浏览器兼容性充其量都是劣质的。 JavaScript 故意不允许操作系统具有这种级别的功能。可以创建一个签名脚本,您会拥有更好的运气,但是......这需要更多的工作,而且几乎不值得。大多数人都知道如何复制和粘贴...

Browser compatibility using any script is shoddy at best. JavaScript intentionally doesn't natively allow this level of functionality with the operating system. 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...

感性 2024-10-01 02:34:28

不幸的是 javascript 没有通用的方法。目前,使用闪光灯和javascript最通用的方式。
查看 LMCButton - 一个用于“复制到剪贴板”的小型动画 Flash 按钮 (4 kb)。

使用 javascript 的示例:

获取按钮的 html 代码:function lmc_get_button(cliptext, idLMC)

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

Unfortunately javascript does not have a universal way. Currently, the use of flash & javascript most universal way.
Look on a LMCButton - a small animated flash button (4 kb) for "Copy to clipboard".

Example of using javascript:

Get html code of the button: function lmc_get_button(cliptext, idLMC)

Update text in the button: function lmc_set_text(idLMC, text)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文