jquery - 如何使 html-clean 不删除 <script> 标签

标签 jquery html

我正在编写文档并想添加一些格式化的 HTML。我使用 htmlClean 进行 HTML 格式化 ( link )。我通过以下方式执行此操作:

HTML代码:

<pre>
    <div> Some more HTML code here </div>
</pre>

JS:

var entityMap = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        '"': '&quot;',
        "'": '&#39;',
        "/": '&#x2F;'
    };

    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 不删除 &lt;script&gt; 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27468345/

相关文章:

php - php中的页面 session ,我想存储用户在特定页面上花费的时间...?

javascript - 如何弹出 chrome 扩展

html - 如何设置 ionic 选项的默认选择值?

javascript - 监听 Reddit 增强套件中的事件

javascript - Jquery 删除 div 内的文本/数字

html - 移动设备扭曲了我网站上的页面

php - URL 中的处理 ID

html - CSS Firefox 与 Chrome 不一致的高度和位置输出

php - jQuery ajax 调用 php 给出 404 not found : what am I missing?

javascript - toggleClass() 在点击标签时未按预期工作