[英]Javascript quote escape giving bugs
I need to print a button to a div that would pass a variable MyVar(string), or, to be precise, it's value, as a parameter to a function addfr(). 我需要将一个按钮打印到div,该div将传递变量MyVar(字符串),或者,确切地说,它是值,作为函数addfr()的参数。 The code: 编码:
document.getElementById("somediv").innerHTML="<button onclick=\"addfr(\""+MyVar+"\")\">Add as friend</button>";
Instead of the expected 而不是预期的
<button onclick="addfr("MyVar")">Add as friend</button>"
I get: 我明白了:
<button onclick="addfr(" MyVar")"="">
What is happening here? 这里发生了什么? Any ideas how to fix this? 任何想法如何解决这一问题?
Edit: The final solution is, for those interested: 编辑:最终解决方案是,对于那些感兴趣的人:
<button onclick=\"addfr(""+MyVar+"")\">Add as friend</button>
It needs to be HTML escaped. 它需要进行HTML转义。 Try using "
尝试使用"
in place of \\"
. 代替\\"
。
您可以改为使用单引号:
document.getElementById("somediv").innerHTML="<button onclick=\"addfr('"+MyVar+"')\">Add as friend</button>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.