将JavaScript显示为代码段 [英] Display javascript as code snippet

查看:130
本文介绍了将JavaScript显示为代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是在页面上显示一段JavaScript代码,而不是让它运行,只是显示为一个代码片段供人们复制。我加载谷歌的Prettify,然后在页面我有这个代码:

What I'm trying to do is display a snippet of javascript on the page, and not have it run, just display as a code snippet for folks to copy. I load google's Prettify, then in the page I have this code:

    <pre class="prettyprint">
    <script>
    $(document).ready(function(){
      setTimeout(function(){
        console.log('deleting cookie');
        $.cookie('cookie',null,{domain: document.domain});
     },1000);
    });
    </script>
   </pre>

但是这段代码只是执行而不显示JS代码片段。

But this code just executes and doesn't display the JS snippet. What am I missing here?

推荐答案

您需要转换< > 字符到HTML实体,如下所示:

You need to convert your < and > characters to HTML entities like so:

<pre class="prettyprint">
  &lt;script&gt;
    $(document).ready(function(){
      setTimeout(function(){
        console.log('deleting cookie');
        $.cookie('cookie',null,{domain: document.domain});
      },1000);
    });
  &lt;/script&gt;
</pre>

我还建议将代码包含在< code> 标签以及现有的< pre> 标签。

I would also recommend wrapping the code in <code> tags in addition to the existing <pre> tags.

这篇关于将JavaScript显示为代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆