【问题标题】:How to display properly formatted JS code snippets on a web page?如何在网页上显示格式正确的 JS 代码片段?
【发布时间】:2016-09-11 12:05:15
【问题描述】:

我将一个 JavaScript 函数存储在没有空格的 DB 中。后来我从数据库中检索并使用 google prettify 在网页上显示,但 code-sn-p 显示在一行中,

var Pizza = function(size, toppingCodes) {
  this.size = size;
  this.toppingCodes = toppingCodes;
  this.toString = function() {
    return "A " + size + " pizza with " + this.toppingCodes + ".";
  };
};
Pizza.prototype.getBaseCost = function() {
  switch (this.size) {
    case "small":
      return 6.50;
    case "medium":
      return 7.50;
    case "large":
      return 8.50;
  }
};

我通过使用 google 美化来获取关键字颜色等。 我可以以格式良好的方式显示上述 JS 函数吗?我们有这个工具/库吗??

【问题讨论】:

    标签: javascript webpage code-snippets prettify


    【解决方案1】:

    我终于明白了。保存到数据库时,我需要使用 encodeURI(string) 对其进行编码,当从服务器取回时,我需要使用 decodeURI(string) 对其进行解码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 2018-02-16
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      相关资源
      最近更新 更多