I am creating a blog using jekyll. I am using prettyprint to highlight the code snippets. I have written a jquery to display a button on hover of the code snippet (inside <pre> tag). On the button click I am getting the entire html of the code snippet but I want to copy the pure text of the code snippet.
我正在使用jekyll创建一个博客。我正在使用prettyprint来突出显示代码片段。我编写了一个jquery来显示代码片段悬停时的按钮(在
标记内)。在按钮上单击我得到代码片段的整个html但我想复制代码片段的纯文本。
Can someone please advise me how to achieve this?
有人可以告诉我如何实现这一目标吗?
1 个解决方案
#1
The document.execCommand
function can be used to copy text to the clipboard in JavaScript. jQuery is not required.
document.execCommand函数可用于在JavaScript中将文本复制到剪贴板。 jQuery不是必需的。
function copy() {
var element = document.getElementById('input');
element.select();
document.execCommand('copy');
element.blur();
}
<input id="input" />
<button onclick="copy()">Copy Text</button>
#1
The document.execCommand
function can be used to copy text to the clipboard in JavaScript. jQuery is not required.
document.execCommand函数可用于在JavaScript中将文本复制到剪贴板。 jQuery不是必需的。
function copy() {
var element = document.getElementById('input');
element.select();
document.execCommand('copy');
element.blur();
}
<input id="input" />
<button onclick="copy()">Copy Text</button>