我试图使用prettify.js
高亮显示我的HTML代码。这是我的HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Making Quines Prettier</title>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?autoload=true&skin=sunburst&lang=HTML" defer="defer"></script>
<style>.operative { font-weight: bold; border:1px solid yellow }</style>
</head>
<body>
<!-- Language hints can be put in XML application directive style comments. -->
<pre class="prettyprint">
<h1>Making Quines Prettier</h1>
<h1>Making Quines Prettier</h1>
</pre>
</body>
</html>
我期待的只是展示
<h1>Making Quines Prettier</h1>
<h1>Making Quines Prettier</h1>
但它呈现HTML代码并将输出显示为:
答案 0 :(得分:3)
您需要转义内容,以便不呈现为html标记
因此代码将被写为
<pre class="prettyprint">
<h1>Making Quines Prettier</h1>
<h1>Making Quines Prettier</h1>
</pre>
请参阅此处的示例:http://jsfiddle.net/s1ckfbsj/1/
修改强>
如果你真的想写html并让它渲染,这里有一个选项:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<pre class="prettyprint">
<h1>Making Quines Prettier</h1>
<h1>Making Quines Prettier</h1>
</pre>
<script>
$(function() {
$('.prettyprint').each(function() {
var $self = $(this);
var html = $self.html();
$self.text(html);
});
});
</script>
请参阅此处的第二个示例:http://jsfiddle.net/s1ckfbsj/2/