Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highlight.js code not working

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 ?

like image 336
mhmd Avatar asked Sep 23 '16 06:09

mhmd


People also ask

How do you highlight in Javascript?

There are many built-in functions in mark. js but we are using two functions for our requirement that is mark() and unmark() function respectively. Here mark() is used to highlight the search text and unmark() is used to remove highlighting the text that is highlighted before.

How do you highlight syntax in HTML?

The <mark> tag defines text that has relevance and is not intended to be used to merely apply highlighter styling. If you wish to style your text to appear highlighted, instead, use a <span> tag with the proper CSS.

What is the use of highlight JS?

Basically, highlight. js lets you easily provide high-quality syntax highlighting without requiring a lot of effort on your part. We're going to show you how to include the code library on any page, and how you can do it in WordPress.


1 Answers

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.

like image 144
WangYudong Avatar answered Oct 03 '22 03:10

WangYudong