简体   繁体   中英

Display code snippet with Javascript

I want to be able to print a code snippet with script-tags using JavaScript.

This is more or less what I want to print:

<script src='URL type='text/javascript'></script><script>functionName();</script>

I tried:

jQuery('selector').text("<pre><script src='URL type='text/javascript'></script><script>functionName();</script></pre>");

Use some syntax higlighter for that (ex https://highlightjs.org/ ).

  <html> <head> <title>test</title> <!-- load highlight.js css & js --> <link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" rel="stylesheet" type="text/css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js" type="text/javascript"></script> </head> <body> <!-- set up your tag which want to highlight --> <pre><code class="xml"> &lt;script src='URL type='text/javascript'>&lt;/script>&lt;script>functionName();&lt;/script> </code></pre> <script> // initialize highlight hljs.initHighlightingOnLoad(); </script> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM