简体   繁体   English

撇号和双引号在Javascript中的问题

[英]issue with apostrophes and double quotes in Javascript

I'm having trouble adding in a variable into the following line of JavaScript: 我在将变量添加到以下JavaScript行时遇到问题:

 row.insertCell(0).innerHTML = "<div onClick='Myfunction(" +  Password +  ");'></div>"

how do I add in the password variable I'm getting confused with apostrophes and double quotes 如何在密码变量中添加撇号和双引号,使我感到困惑

I think it needs to put the value in-between apostrophes but this clashes with what's already there? 我认为它需要将值放在撇号之间,但这与已经存在的冲突?

试试这个: row.insertCell(0).innerHTML = "<div onClick=Myfunction('" + Password + "');></div>"

您可以尝试使用反斜杠这样的转义引号

row.insertCell(0).innerHTML = "<div onClick='Myfunction(\"" +  Password +  "\");'></div>"

Try this example: 试试这个例子:

 var Password ='sample';

 document.getElementById("id1").value= '<div onClick="Myfunction(\'' +  Password +  '\');"></div>';

 alert(document.getElementById("id1").value);

This is called Escaping. 这称为转义。 Use backslash() for the character which you want to escape. 对要转义的字符使用反斜杠()。

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

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