简体   繁体   English

使用Javascript显示代码段

[英]Display code snippet with Javascript

I want to be able to print a code snippet with script-tags using JavaScript. 我希望能够使用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/ ). 使用一些语法高亮(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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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