我搜索了有关“未终止的字符串文字”语法错误的许多问题,但没有发现任何帮助我...
所以,我有这个Javascript函数:
function parametresModal($parametres) { document.getElementById('remodalTest').innerHTML = $parametres; };然后我在我页面的链接上调用此函数:
<a href="#" onClick='parametresModal("<?php the_field('description-defi'); ?>");'>TEST</a>这里写的参数是简化的; 我实际上想要添加这个Wordpress ACF的功能和HTML标记,但我发现问题出现在这个特定领域(见下文)。
这个“parametresModal”函数应该用它的参数填充下面的div:
<div id="remodalTest">MyDiv</div>问题是控制台输出
“SyntaxError:unterminated string literal”
Wordpress ACF的字段“description-defi”包含几行文字和一些简单的引号(例如c'est,l'éviter,...)。 所以我尝试用几种方法来逃避引号:
$myField = the_field('description-defi'); $myEscape = json_encode($myField);要么
$myField = the_field('description-defi'); $myEscape = addshlashes($myField);要么
$myField = the_field('description-defi'); $myEscape = htmlspecialchars($myField);总是导致相同的错误。
你看到我的代码或我的思维方式可能出错吗?
非常感谢你 !
I search through the numerous questions already asked about the "unterminated string literal" syntax error but found nothing helping me ...
So, I have this Javascript function :
function parametresModal($parametres) { document.getElementById('remodalTest').innerHTML = $parametres; };Then I call this function on a link in my page :
<a href="#" onClick='parametresModal("<?php the_field('description-defi'); ?>");'>TEST</a>The parameter written here is simplified ; I actually want to add this Wordpress ACF's function among others and HTML markup, but I found the issue was appearing with this particular field (see below).
This "parametresModal" function is supposed to fill the following div with its parameters :
<div id="remodalTest">MyDiv</div>Problem is the console outputs
"SyntaxError: unterminated string literal"
The Wordpress ACF's field "description-defi" contains a few lines of text with some simple quotes (ex. c'est, l'éviter, ...). So I tried to escape the quotes with several methods :
$myField = the_field('description-defi'); $myEscape = json_encode($myField);or
$myField = the_field('description-defi'); $myEscape = addshlashes($myField);or
$myField = the_field('description-defi'); $myEscape = htmlspecialchars($myField);Always resulting in the same error.
Do you see where I could be wrong in my code or my way of thinking the thing ?
Thank you very much !
最满意答案
the_field()将输出所选字段的内容。 如果要使用字段,则应使用get_field() 。
请参阅: https : //www.advancedcustomfields.com/resources/the_field/
此外,换行字符也不会被任何PHP的转义函数转义,如果你的String包含换行符,你需要使用以下内容手动转义它们: $myField = str_replace(array("\r\n", "\n"), "<br>", $myField); 。
如果您知道您的数据库将始终使用相同的换行符序列,则可以使用该换行符序列替换array("\r\n", "\n") 。
the_field() will output the content of the selected field. If you want to work with a field, you should use get_field() instead.
See: https://www.advancedcustomfields.com/resources/the_field/
Also the newline character will not be escaped by any of PHP's escape functions, if your String contains newlines, you will need to escape them manually using something like this: $myField = str_replace(array("\r\n", "\n"), "<br>", $myField);.
If you know that your DB will consistently use the same newline sequence, you can replace array("\r\n", "\n") by that newline sequence instead.
更多推荐
发布评论