js 从样式化的代码元素中复制干净的代码
js copy clean code from styled code element
我正在使用highlight.js
,生成了一个区块:
<pre class="code code-javascript"><label>JS</label><code class="language-javascript">equation_app.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">e</span>=></span>{
autofocus(e);
<span class="hljs-keyword">let</span> start = editor.selectionStart;
<span class="hljs-keyword">let</span> end = editor.selectionEnd;
tagging(<span class="hljs-string">'$'</span>, <span class="hljs-string">'$'</span>);
editor.focus();
<span class="hljs-keyword">if</span>(editor.selectionStart === editor.selectionEnd)editor.selectionStart = editor.selectionEnd = start + <span class="hljs-number">1</span>;
refresh();
});</code><div class="copy">click to copy</div></pre>
我做了一个按钮,点击它可以将文本复制到用户的剪贴板。
function copy_to_clipboard(stritem){
const el = document.createElement('textarea');
el.value = stritem;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
window.alert('Successfully copied to your clipboard!');
}
但是,我无法直接调用 copy_to_clipboard(codeElement.innerHTML)
,因为所有 html 样式内容都已复制。
我想知道是否有一种方法可以从样式化的 Dom <code>
元素中提取干净的代码
而不是使用 innerHTML
使用 textContent
如:
function copy_to_clipboard() {
const el = document.createElement('textarea');
el.value = document.querySelector('code').textContent;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
window.alert('Successfully copied to your clipboard!');
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<pre class="code code-javascript">
<label>JS</label>
<code class="language-javascript">
equation_app.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">e</span>=></span>{
autofocus(e);
<span class="hljs-keyword">let</span> start = editor.selectionStart;
<span class="hljs-keyword">let</span> end = editor.selectionEnd;
tagging(<span class="hljs-string">'$'</span>, <span class="hljs-string">'$'</span>);
editor.focus();
<span class="hljs-keyword">if</span>(editor.selectionStart === editor.selectionEnd)editor.selectionStart = editor.selectionEnd = start + <span class="hljs-number">1</span>;
refresh();
});
</code>
<div class="copy" onclick="copy_to_clipboard()">click to copy</div>
</pre>
参考:
我正在使用highlight.js
,生成了一个区块:
<pre class="code code-javascript"><label>JS</label><code class="language-javascript">equation_app.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">e</span>=></span>{
autofocus(e);
<span class="hljs-keyword">let</span> start = editor.selectionStart;
<span class="hljs-keyword">let</span> end = editor.selectionEnd;
tagging(<span class="hljs-string">'$'</span>, <span class="hljs-string">'$'</span>);
editor.focus();
<span class="hljs-keyword">if</span>(editor.selectionStart === editor.selectionEnd)editor.selectionStart = editor.selectionEnd = start + <span class="hljs-number">1</span>;
refresh();
});</code><div class="copy">click to copy</div></pre>
我做了一个按钮,点击它可以将文本复制到用户的剪贴板。
function copy_to_clipboard(stritem){
const el = document.createElement('textarea');
el.value = stritem;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
window.alert('Successfully copied to your clipboard!');
}
但是,我无法直接调用 copy_to_clipboard(codeElement.innerHTML)
,因为所有 html 样式内容都已复制。
我想知道是否有一种方法可以从样式化的 Dom <code>
元素中提取干净的代码
而不是使用 innerHTML
使用 textContent
如:
function copy_to_clipboard() {
const el = document.createElement('textarea');
el.value = document.querySelector('code').textContent;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
window.alert('Successfully copied to your clipboard!');
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<pre class="code code-javascript">
<label>JS</label>
<code class="language-javascript">
equation_app.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">e</span>=></span>{
autofocus(e);
<span class="hljs-keyword">let</span> start = editor.selectionStart;
<span class="hljs-keyword">let</span> end = editor.selectionEnd;
tagging(<span class="hljs-string">'$'</span>, <span class="hljs-string">'$'</span>);
editor.focus();
<span class="hljs-keyword">if</span>(editor.selectionStart === editor.selectionEnd)editor.selectionStart = editor.selectionEnd = start + <span class="hljs-number">1</span>;
refresh();
});
</code>
<div class="copy" onclick="copy_to_clipboard()">click to copy</div>
</pre>
参考: