tags:javascript
对网页上的内容实现复制粘贴的功能css
痛点:须要支持多种不一样的浏览器 主要有IE,Firefoxhtml
window.clipboardData.setData("Text", text);
document.execCommand('copy')
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
弹出框内容为选中的文案,用户按下ctrl+c 实现复制整合以后的代码为java
function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData("Text", text);
} else {
var textArea = document.createElement("textarea");
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
if (!document.execCommand('copy')) {
copyToClipboardMozilla(text);
} else {
showInfo("提示", "复制成功")
}
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
}
function copyToClipboardMozilla(text) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
$(".copy").on("mouseenter", function () {
$(this).css("background-color", "#c8c9c8");
$(this).focus();
var textArea = document.createElement("textarea");
textArea.style.background = 'transparent';
textArea.id = "copyContent";
textArea.value = $(this).text();
document.body.appendChild(textArea);
textArea.select();
})
$(".copy").on("mouseleave", function () {
$(this).css("background-color", "");
document.body.removeChild(document.getElementById("copyContent"));
})
复制代码
参考资料:git
兼容处理了浏览器的复制功能,有更好的方案解决欢迎留言联系 未经做者容许 请勿转载,谢谢 :)github