gpt4 book ai didi

javascript - 如何使用 Javascript 为 asp :label? 执行 "select all"和 "copy to clipboard"

转载 作者:行者123 更新时间:2023-11-29 15:51:29 25 4
gpt4 key购买 nike

我想使用 javascript 复制 asp:label 的内容。

我可以用这个方法来做:

strContent = document.getElementById('MainContent_lblHtml').innerText;
window.clipboardData.setData("Text", strContent);

但它会去除格式并仅复制文本。 (我假设是因为数据格式设置为“文本”。)

标签包含一些格式化的 html。我想保留格式,获得与用鼠标在屏幕上突出显示它,然后复制到(例如)word 文档中一样的效果。

最佳答案

已更新

以下将突出显示所需的 div,然后将 HTML 复制到剪贴板。转到 Word 并按 CTRL+V 将格式化的 html 粘贴到文档中。

<script type="text/javascript">
function CopyHTMLToClipboard() {
if (document.body.createControlRange) {
var htmlContent = document.getElementById('MainContent_lblHtml');
var controlRange;

var range = document.body.createTextRange();
range.moveToElementText(htmlContent);

//Uncomment the next line if you don't want the text in the div to be selected
range.select();

controlRange = document.body.createControlRange();
controlRange.addElement(htmlContent);

//This line will copy the formatted text to the clipboard
controlRange.execCommand('Copy');

alert('Your HTML has been copied\n\r\n\rGo to Word and press Ctrl+V');
}
}
</script>

关于javascript - 如何使用 Javascript 为 asp :label? 执行 "select all"和 "copy to clipboard",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5117442/

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