jquery 一键复制input内容
您可以使用以下代码来实现一键复制input内容的功能:
function copyInputContent() {
// 获取input元素
var inputElement = document.getElementById("inputId");
// 选择input内容
inputElement.select();
// 复制选择的内容
document.execCommand("copy");
// 取消选择
window.getSelection().removeAllRanges();
}
其中,"inputId"是您想要复制内容的input元素的id。您可以在需要的地方调用copyInputContent()函数来触发复制操作。
请注意,该方法使用了document.execCommand("copy")来复制内容,这在大多数现代浏览器中都是可行的,但在某些浏览器中可能不起作用。因此,最好在使用此方法之前进行兼容性检查,并提供一个备用方案。
原文地址: https://www.cveoy.top/t/topic/jfMK 著作权归作者所有。请勿转载和采集!