javascript - 调用函数时在我的 JS 代码中未捕获的 SyntaxError
问题描述
我正在尝试为我的 xslt 编写一个 JS 函数,但我在调用我的 JS 函数时'' string literal contains an unescaped line break
在线收到此错误。number[cnt] = '<xsl:value-of select="termsandconditions" />';
我不确定为什么会收到此错误。欢迎任何帮助或建议。谢谢!
Script
function myFunction() {
var number = new Array();
cnt=0;
<xsl:choose>
<xsl:when test="//faml/response/termsandconditonsresponsedto/terms_conditons">
<xsl:for-each select="//faml/response/termsandconditonsresponsedto/terms_conditons/tandcdto">
number[cnt] = '<xsl:value-of select="termsandconditions" />';
alert(number[cnt]);
</xsl:for-each>
</xsl:when>
</xsl:choose>
document.getElementById("myText").innerHTML = number[cnt];
}
解决方案
如果这是现代浏览器执行的 Javascript,则使用反勾号:
number[cnt] = `<xsl:value-of select="termsandconditions"/>`;
我不认为 IE 支持这一点。如果您需要 IE 支持,则编写 XSLT 以转义 中的任何换行符和单引号termsandconditions
,这在 XSLT 2 和更高版本中很容易,replace
在 XSLT 1 中更乏味,您需要命名模板或支持扩展功能。