Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
js copia el código limpio del elemento de código con estilo

Estoy usando highlight.js , generé un bloque:

 <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>=&gt;</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>

Hice un botón para hacer clic para copiar el texto en el portapapeles del usuario.

 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!'); }

Sin embargo, no puedo llamar directamente a copy_to_clipboard(codeElement.innerHTML) ya que todas las cosas de estilo html se copian.

Me pregunto si hay alguna forma de extraer código limpio del elemento Dom <code> con estilo

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En lugar de usar innerHTML , use textContent como:

 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>=&gt;</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>

Referencia:

  • contenido del texto
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!