我想要一个按钮,它可以选择
有一个非常新的选项,它是跨浏览器的,但需要时间让每个人更新其浏览器。
它通过使用document.execCommand('copy');
函数来实现。使用此函数,您将复制所选文本。这不仅适用于textarea
,还适用于页面上选择的所有文本(例如span
,p
,div
等)。
在Internet Explorer 10+,Chrome 43+,Opera 29+和Firefox 41+中可用(请参见此处的execCommand
兼容性)。
// Setup the variables
var textarea = document.getElementById("textarea");
var answer = document.getElementById("copyAnswer");
var copy = document.getElementById("copyBlock");
copy.addEventListener('click', function(e) {
// Select some text (you could also create a range)
textarea.select();
// Use try & catch for unsupported browser
try {
// The important part (copy selected text)
var ok = document.execCommand('copy');
if (ok) answer.innerHTML = 'Copied!';
else answer.innerHTML = 'Unable to copy!';
} catch (err) {
answer.innerHTML = 'Unsupported Browser!';
}
});
<textarea id="textarea" rows="6" cols="40">
Lorem ipsum dolor sit amet, eamsemper maiestatis no.
</textarea><br/>
<button id="copyBlock">Click to copy</button> <span id="copyAnswer"></span>
此回答虽然在2011年准确无误,但现在已经过时了。 请参阅arc的答案,或https://dev59.com/fHRC5IYBdhLWcg3wJNcN#30810322
如果您不想使用Flash插件,则必须使用它来自动将文本复制到客户端的剪贴板。浏览器是这样设计的,因为网站自动修改客户端的剪贴板而没有活动-x组件的帮助是一个安全问题。请注意,活动-x组件是在用户计算机上运行的程序,技术上需要用户的同意才能安装。考虑到剪贴板是操作系统组件,很高兴网络浏览器默认不允许网站劫持它。
如果用户没有Flash、禁用了Flash或禁用了活动-X,则他或她可能对安全非常警惕,并且不希望您干扰他或她的键盘。在这一点上,用户习惯于在网站中没有太多自动或基于脚本的功能。最好不要试图公开违抗终端用户的意愿。
请参考以下Stack Overflow链接:
最终的答案是使用Zero Clipboard,它是一个库,使用小型、不可见的Flash电影和JavaScript来使用您想要的剪贴板功能。该库可在此处获取:https://github.com/zeroclipboard/zeroclipboard第二个链接显示如何检测Flash是否禁用或未安装,这样您就可以像使用JavaScript一样显示警告消息。
<script src="path_to_script/clipboard.min.js"></script>
在你的 script.js 文件上实例化一个新的触发器
new Clipboard('.trigger');
请前往以下链接查看使用示例:http://zenorocha.github.io/clipboard.js/#usage
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
textArea.id = 'ta';
document.body.appendChild(textArea);
//textArea.select();
var range = document.createRange();
range.selectNode(textArea);
textArea.select();
window.getSelection().addRange(range);
try {
var successful = document.execCommand('copy');
} catch (err) {
alert('Oops, unable to copy');
}
document.body.removeChild(textArea);
return successful;
}
我希望这能有所帮助。
function CopyString(){
var StringToCopyElement = document.getElementById('YourId');
StringToCopyElement.select();
if(document.execCommand('copy')){
StringToCopyElement.blur();
}else{
CopyStringMobile();
}
}
function CopyStringMobile(){
document.getElementById("YourId").selectionStart = 0;
document.getElementById("YourId").selectionEnd = 999;
document.execCommand('copy');
if (window.getSelection) {
if (window.getSelection().empty) { // Chrome
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) { // Firefox
window.getSelection().removeAllRanges();
}
} else if (document.selection) { // IE?
document.selection.empty();
}
}
我在浏览器中使用剪贴板属性。
const text = "THE CONTENT THAT YOU WANT TO COPY TO CLIPBOARD";
// check if navigator has the clipboard property
if(navigator.clipboard){
await navigator.clipboard.writeText(text);
}
不支持的浏览器!
- Madhawa Priyashanthatextarea.select();
中的textarea
。 - MForMarlon