如何使html-clean不删除<script>标记</script>

时间:2014-12-14 10:20:34

标签: 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格式化脚本可能是一个解决方案。

提前致谢!

2 个答案:

答案 0 :(得分:0)

根据您提供的链接中的文档,有一个名为“allowedTags”的选项。因此,您必须将该选项传递给$ .htmlClean;

$.htmlClean(code, {
    allowedTags: ["pre", "script"],
    format: true,
    formatIndent: 0
});

希望这个答案很有用。祝你好运

答案 1 :(得分:0)

Sub Test2()

    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlSheet As Object
    Dim enviro As String
    Dim strPath As String

    enviro = CStr(Environ("USERPROFILE"))

    'the path of the workbook
    strPath = enviro & "\Documents\test1.xlsx"

    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err <> 0 Then
        Application.StatusBar = "Please wait while Excel source is opened ... "
        Set xlApp = CreateObject("Excel.Application")
        bXStarted = True
    End If
    On Error GoTo 0

    'Open the workbook to input the data
    Set xlWB = xlApp.Workbooks.Open(strPath)
    Set xlSheet = xlWB.Sheets("Sheet1")
    ' Process the message record
    Dim rw As Range
    Dim RowCount As Integer

    RowCount = 3

    For Each rw In xlSheet.Rows
        Debug.Print xlSheet.Cells(rw.Row, 1).Value
    Exit For

    RowCount = RowCount + 1

Next rw

xlWB.Close 1
If bXStarted Then
    xlApp.Quit
End If
End Sub
我使用的jquery.clean版本是1.4.2