我正在编写文档并想添加一些格式化的 HTML。我使用 htmlClean 进行 HTML 格式化 ( link )。我通过以下方式执行此操作:
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
});
希望这个答案有用。祝你好运
关于jquery - 如何使 html-clean 不删除 <script> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27468345/