如何在html / css / js / php中格式化代码 [英] How to format code in html / css / js/ php
问题描述
我正在寻找一种方法来自动格式化我在HTML文档中编写的代码。我知道维基百科这样做,例如在页面上: http://en.wikipedia.org/wiki/Nested_function
I'm looking for a way to automatically format and color code I write in an HTML document. I know wikipedia does it, for example on the page: http://en.wikipedia.org/wiki/Nested_function
我确信有图书馆可以做到这一点,但我无法为我的生活找到一个。有没有人有任何建议?
I'm sure there are libraries out there to do this, but I can't for the life of me, find one. Does anyone have any suggestions?
推荐答案
查看美化JavaScript 库。这是人们通常使用的一种方式(例如,这是在SO上使用的方式)。
Have a look at the Prettify JavaScript library. It's the one generally used by people (it's the one being used here on SO, for example.)
您可以像这样使用它:
在您的< head>
元素中:
In your <head>
element:
<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script>
在< body>
元素:
In your <body>
element:
<body onload="prettyPrint()">
<!-- any HTML you like here... -->
<pre class="prettyprint">
def say_hi():
print("Hello World!")
</pre>
<!-- any HTML you like here... -->
</body>
这是为了简单的使用库。如果您在页面上使用其他JavaScript,我会推荐其他启用Prettify库的方法(即,不要使用的
元素)。例如,如果您使用的是jQuery,我写了这个jQuery插件,我通常用它来突出显示某些元素: onload
属性>< body>
That's for simple use of the library. If you're using other JavaScript on your page I would recommend other methods for enabling the Prettify library (i.e., don't use the onload
attribute of the <body>
element.) For example, if you're using jQuery, I wrote this jQuery plugin that I usually use to syntax highlight certain elements:
// Extend jQuery functionality to support prettify as a prettify() method.
jQuery.fn.prettify = function () { this.html(prettyPrintOne(this.html())); };
使用方法如下:
Used like this:
$('#my-code-element').prettify();
这篇关于如何在html / css / js / php中格式化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!