我想做的是在页面上显示一个 javascript 片段,而不是让它运行,只是显示为代码片段供人们复制。我加载了谷歌的 Prettify,然后在页面中我有这段代码:
<pre class="prettyprint">
<script>
$(document).ready(function(){
setTimeout(function(){
console.log('deleting cookie');
$.cookie('cookie',null,{domain: document.domain});
},1000);
});
</script>
</pre>
但是这段代码只是执行,并没有显示 JS 片段。我在这里缺少什么?
最佳答案
您需要转换您的 <
和 >
字符到 HTML 实体,如下所示:
<pre class="prettyprint">
<script>
$(document).ready(function(){
setTimeout(function(){
console.log('deleting cookie');
$.cookie('cookie',null,{domain: document.domain});
},1000);
});
</script>
</pre>
我还建议将代码包装在 <code>
中除了现有的 <pre>
标签标签。
关于javascript - 将 javascript 显示为代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17753241/