Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
渲染速度线中断导致JavaScript错误_Javascript_Velocity_Line Breaks_Liferay Velocity_Liferay Aui - Fatal编程技术网

渲染速度线中断导致JavaScript错误

渲染速度线中断导致JavaScript错误,javascript,velocity,line-breaks,liferay-velocity,liferay-aui,Javascript,Velocity,Line Breaks,Liferay Velocity,Liferay Aui,我在velocity模板中使用JavaScript代码,但它不起作用 我使用此模板阅读内容,并希望将其设置为js变量,但内容中有换行符,我得到以下错误: SyntaxError:未终止的字符串文字 在呈现的代码中,您可以看到错误: var exampleText = 'This is the first line and this is the second line.'; 在原始代码中,它是这样编写的: var exampleText = '$question.answer.dat

我在velocity模板中使用JavaScript代码,但它不起作用

我使用此模板阅读内容,并希望将其设置为js变量,但内容中有换行符,我得到以下错误:

SyntaxError:未终止的字符串文字

在呈现的代码中,您可以看到错误:

var exampleText = 'This is the first line

and this is the second line.';
在原始代码中,它是这样编写的:

    var exampleText = '$question.answer.data';
var regularPanels = new A.Panel( { 
    bodyContent: exampleText, 
    collapsible: true, 
    collapsed: true,
    headerContent: '$question.data' } ) .render('#regularPanels$counter$reserved-article-id.data$randomNamespace');  

});
是否有可能忽略js编译的换行符,但仍在完整的呈现页面上显示它

好的,我在速度的帮助下解决了这个问题

结合emiliocai的回答,以下代码运行良好:

<div id="example-text" style="display:none;">
   <p>$escapeTool.java($question.answer.data).replace("\n","<br />")</p>
</div>

<script type="text/javascript" charset="utf-8">

AUI().ready('aui-panel', function(A) { 

var exampleText = document.getElementById('example-text').innerHTML;
var regularPanels = new A.Panel( { 
    bodyContent: exampleText, 
    collapsible: true, 
    collapsed: true,
    headerContent: '$question.data' } ) .render('#regularPanels$counter$reserved-article-id.data$randomNamespace');  

});

</script>
它可能在没有隐藏标签的情况下工作,但我还没有测试它

因此,也可能是:

var exampleText = '$escapeTool.java($question.answer.data).replace("\n","<br />")';

测试它->工作

很抱歉,JavaScript中不接受换行符,不确定模板的外观,但如果您确实无法用替换换行符\n或替换换行符,您可以采取以下措施:

在模板中,将来自数据库的内容呈现到隐藏的div中:

$question.answer.data

在javascript代码中,将div的内容读入变量:

var exampleText=document.getElementById'example-text'.innerHTML


不确定我是否理解,但您不能使用或\n代替换行符吗?这就是问题所在。我希望如此,但这些换行是由模板生成的。在数据库中,内容question.answer.data与新行的-标记一起保存。没关系,但我认为在内容进入脚本函数之前,它们将被编译成新行。我试图用很多方法捕捉和替换换行符,但找不到解决方案。谢谢:我已经知道换行是不可接受的,但是速度似乎不知道:'如果没有错误,内容显示出来,但现在所有的都显示在一行中。是否存在转义或捕获换行符的可能性?您可以尝试替换如下内容的\n:var exampleText=document.getElementById'example-text'.innerHTML.replace\n,;我已经试过更换了,但还是不起作用。如果我知道velocity使用的是哪种换行符,那就容易多了,因为它既不是\n也不是存储在数据库中。