我正在编写一个文档,想添加一些格式化的HTML。我使用htmlClean进行HTML格式化(链接)。我通过以下方式执行此操作:
网页代码:
<pre>
<div> Some more HTML code here </div>
</pre>
.JS:
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
function htmlEncode(string) {
return String(string).replace(/[&<>"'/]/g, function (s) {
return entityMap[s];
});
}
$("pre").each(function () {
var code = $(this).html();
$(this).empty();
code = $.htmlClean(code, {
format: true,
formatIndent: 0
})
//code = htmlEncode(code)
$(this).html(code);
});
它工作得很好。问题是当我想在格式化代码中有一个脚本标签时,如下所示:
<pre>
<script type="text/javascript" src="js/tabs-accordions.min.js"></script>
</pre>
结果是空字段,我不知道为什么。我试图调试它但没有成功。
请如此友善,并提出一个解决方案,或者更好的 JS HTML 格式脚本可能是一个解决方案。
提前感谢!
根据您提供的链接中的文档,有一个名为"allowedTags"的选项。因此,您必须将该选项传递给 $.htmlClean;
$.htmlClean(code, {
allowedTags: ["pre", "script"],
format: true,
formatIndent: 0
});
希望这个答案有用。祝你好运
<script type="text/javascript" src="js/tabs-accordions.min.js">
</script>
我使用的jquery.clean版本是1.4.2