繁体   English   中英

在JavaScript函数中转义单引号

[英]Escaping single quotes in javascript function

我正在使用fn:escapeXml在javascript函数中转义引号。 这与双引号完美配合,但对单引号无效。 以下是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> 

${listItem.getBaseRate()}包含单引号时,将发生错误。 Uncaught SyntaxError: missing ) after argument list出现错误Uncaught SyntaxError: missing ) after argument list有人可以帮我吗

函数参数中的模板文字不起作用。 您将只需要提供参数。

例如:

myFunc(`${myVar}`)

可以简单地使用:

myFunc(myVar)

另外,请勿按评论中的某些性能建议附加内联处理程序。

包含传递参数的示例

 <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