简体   繁体   English

在JavaScript函数中转义单引号

[英]Escaping single quotes in javascript function

I am using fn:escapeXml to escape quotes in a javascript function. 我正在使用fn:escapeXml在javascript函数中转义引号。 This works perfectly with double quotes however it doesn't work with single quotes. 这与双引号完美配合,但对单引号无效。 Below is the html code: 以下是html代码:

 <button class = "location" 
   onclick = "locationModelMethod('${fn:escapeXml(listItem.getBaseRate())}','${listItem.getCreateDate()}','${listItem.getId()}','${listItem.getUser().getEmployeeId()}','${listItem.getChecker().getEmployeeId()}','${listItem.getStatus()}', '${listItem.getRemarks()}' ,${listItem.isActive()})" >
${fn:escapeXml(listItem.getBaseRate())}
 </button> 

The error occurs when ${listItem.getBaseRate()} contains a single quote. ${listItem.getBaseRate()}包含单引号时,将发生错误。 I am getting an error Uncaught SyntaxError: missing ) after argument list Can anybody help me out with this Uncaught SyntaxError: missing ) after argument list出现错误Uncaught SyntaxError: missing ) after argument list有人可以帮我吗

Template literals inside function parameters doesn't work. 函数参数中的模板文字不起作用。 You will need to simply provide parameters. 您将只需要提供参数。

For example: 例如:

myFunc(`${myVar}`)

Can be simply used: 可以简单地使用:

myFunc(myVar)

Also, don't attach inline handler as suggested by CertainPerformance in the comment. 另外,请勿按评论中的某些性能建议附加内联处理程序。

Example including parameters being passed 包含传递参数的示例

 <html> <body> <script> // Parameters that get passed into function being fired by onclick var myParam = 'param'; function myParameterFunction() { return 'functionParam'; } function myParameterFunctionWithParams(foo) { return foo + 1; } </script> <button onclick="myFunction(myParam, myParameterFunction(), myParameterFunctionWithParams(2))"> Click me! </button> <script> function myFunction(a, b, c) { console.log('fired', a, b, c) } </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM