将字符串值传递到包含特殊字符的 JavaScript 函数中

pass string values into javascript function that contains special characters

本文关键字:JavaScript 函数 特殊字符 包含 字符串 值传      更新时间:2023-09-26

嗨,你能告诉我如何将以下值传递给javascript函数参数吗?

我从其中一个 Java 字符串变量中动态获取此值,例如:

String vals= "The apostrophe ( ’ or ' ) is a punctuation < ! ^ & *mark,'";

应该在这个函数 argumentshowPopUpMsgBanner 中解析

<html>
   <body>
 <button onclick="showPopUpMsgBanner('<%=vals%>')" >Click me</button>
 <script>
  function showPopUpMsgBanner(args){
 alert('values '+args);
}
</script>
</body>
</html>

只需转义最里面的单引号'

 <button onclick="showPopUpMsgBanner('The apostrophe ( ’ or '' ) is a punctuation < ! ^ & *  mark,')" >Click me</button>

另外,由于您直接传递值,因此无需访问其value属性

 function showPopUpMsgBanner(args){
    alert('values '+args);
 }