大于 > 小于 < inside thymeleaf javascript... 错误:元素的内容必须由格式正确的字符数据或标记组成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29540489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:41:35  来源:igfitidea点击:

Greater than > Less than < inside thymeleaf javascript... Error: The content of elements must consist of well-formed character data or markup

javajavascripthtmlspringthymeleaf

提问by Faraj Farook

I get this error when I try to insert <or >operators inside the thymeleafjavascript.

当我尝试在javascript 中插入<>操作符时出现此错误thymeleaf

My code

我的代码

<script th:inline="javascript">
    $(document).ready(function () {
        ...
        if(timeRemain < 0){
            ...
        }
        ...
        var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
        ...         
    });
</script>

Error message

错误信息

org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.

org.xml.sax.SAXParseException:元素的内容必须由格式正确的字符数据或标记组成。

How can i solve this?

我该如何解决这个问题?

回答by Faraj Farook

This issue is solved by adding CDATAto the scripttag as below

这个问题是通过添加CDATAscript标签来解决的,如下所示

<script th:inline="javascript">
    /*<![CDATA[*/
    ...  
    ...
    ...
    /*]]>*/
</script>

回答by Rahul Tripathi

Apart from using CDATA you can also encode < as &lt;and > as &gt;

除了使用 CDATA,您还可以编码< as &lt;> as &gt;