【发布时间】:2018-03-12 13:10:16
【问题描述】:
我一直在尝试将<span> 的innerContent 复制到我的剪贴板,但没有成功:
HTML
<span id="pwd_spn" class="password-span"></span>
JavaScript
函数调用
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('copy').addEventListener('click', copy_password);
});
功能
function copy_password() {
var copyText = document.getElementById("pwd_spn").select();
document.execCommand("Copy");
}
我也试过了:
function copy_password() {
var copyText = document.getElementById("pwd_spn").textContent;
copyText.select();
document.execCommand("Copy");
}
似乎.select() 不适用于<span> 元素,因为我在两者上都收到以下错误:
【问题讨论】:
标签: javascript html copy clipboard copy-paste