gpt4 book ai didi

javascript - 将整个 html 表复制到剪贴板 javascript

转载 作者:搜寻专家 更新时间:2023-11-01 04:54:14 32 4
gpt4 key购买 nike

我已经编写了 javascript 来选择表格,但我现在想在单击按钮后自动复制它。请帮助我。我的 javascript 是这样的。

function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
document.execCommand('Copy');
} catch (e) {
range.selectNode(el);
sel.addRange(range);
document.execCommand('Copy');
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
range.execCommand('Copy');

}
}

最佳答案

function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
document.execCommand("Copy");}

关于javascript - 将整个 html 表复制到剪贴板 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053004/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com