最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - highlight.js code not working - Stack Overflow

programmeradmin1浏览0评论

I try to use the highlight.js but it didn't work

i work like they say in the website but i don't know what's wrong

<link rel="stylesheet" href="styles/default.css">
<script src="js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
 <title></title>  
 </head>

//test the code

 <pre><code class="html"><input type="text" name="test" id="test" value=""></code></pre>

<link rel="stylesheet" href="//cdnjs.cloudflare/ajax/libs/highlight.js/9.7.0/styles/default.min.css">
<script src="//cdnjs.cloudflare/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>

the result in the browser is a normal textbox not the code how to solve this ?

I try to use the highlight.js but it didn't work

i work like they say in the website but i don't know what's wrong

<link rel="stylesheet" href="styles/default.css">
<script src="js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
 <title></title>  
 </head>

//test the code

 <pre><code class="html"><input type="text" name="test" id="test" value=""></code></pre>

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>

the result in the browser is a normal textbox not the code how to solve this ?

Share Improve this question edited Sep 23, 2016 at 7:07 mhmd asked Sep 23, 2016 at 6:29 mhmdmhmd 2542 gold badges3 silver badges14 bronze badges 15
  • what are you trying to highlight? – Martin Gardener Commented Sep 23, 2016 at 6:30
  • Any live demo to show your problem? – WangYudong Commented Sep 23, 2016 at 6:31
  • @DanyalImran i put it under test code – mhmd Commented Sep 23, 2016 at 6:33
  • @WangYudong the problem like i say it didn't show the code it run the code show the output of the code – mhmd Commented Sep 23, 2016 at 6:34
  • 1 Have you included your .css and .js file correctly? Any error shows in browser console? If you have a live website, it's more easier for us to resolve your problem. – WangYudong Commented Sep 23, 2016 at 6:37
 |  Show 10 more comments

2 Answers 2

Reset to default 19

The reason your HTML code is not treated as code by highlight.js is because the browser parsed the HTML tags. The solution is to replace < with &lt; and > with &gt; to escape angle brackets. If you included your .js and .css file correctly, make these change will help:

HTML version:

<pre><code class="html">&lt;input type="text" name="test" id="test" value=""&gt;</code></pre>

PHP version:

<pre><code class="php">&lt;?php echo"test";?&gt;</code></pre>

BTW, no need to use jQuery if you include .js file in HTML <head>. The script will run after the page loaded.

In a case if you're using php on backend you can use php function htmlspecialchars to convert special symbols programatically.

<pre><code class='language-php'>
<?php echo htmlspecialchars( 
'<input type="text" name="test" id="test" value="">' );
?>
</code></pre>
发布评论

评论列表(0)

  1. 暂无评论