当值设置为代码镜像时,如何格式化代码镜像中的HTML代码?

Lak*_*ana 4 codemirror

我正在使用Code Mirror插件来编辑页面的HTML源代码.HTML代码从数据库中获取并在代码镜像中设置为值.但是,设置值后,它将以与数据库中保存的格式相同的格式显示.如何以正确的格式显示?提前致谢.

Fra*_*len 10

有一个叫做的函数 autoFormatRange

editor.autoFormatRange(range.from, range.to);
Run Code Online (Sandbox Code Playgroud)

CodeMirror Group中的这个片段可能就是您所需要的:

function autoFormat() {
    var totalLines = editor.lineCount();
    var totalChars = editor.getTextArea().value.length;
    editor.autoFormatRange({line:0, ch:0}, {line:totalLines, ch:totalChars});
}
Run Code Online (Sandbox Code Playgroud)

http://codemirror.net/2/demo/formatting.html

  • autoFormatRange方法似乎已经消失了 (5认同)
  • 是的,ty.However自3.11以来已删除:https://groups.google.com/forum/?fromgroups#!searchin/codemirror /how%2420to%2420manage%2420formatting/codemirror/hdEWC83r94c /PcaVazYL9PAJ (3认同)