在 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
谁能帮我解决这个问题
函数参数中的模板文字不起作用。您只需提供参数即可。
例如:
myFunc(`${myVar}`)
可以简单使用:
myFunc(myVar)
此外,不要按照 CertainPerformance 在评论中的建议附加内联处理程序。
包含传递参数的示例
<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>
我正在使用 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
谁能帮我解决这个问题
函数参数中的模板文字不起作用。您只需提供参数即可。
例如:
myFunc(`${myVar}`)
可以简单使用:
myFunc(myVar)
此外,不要按照 CertainPerformance 在评论中的建议附加内联处理程序。
包含传递参数的示例
<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>