在带有字符串的onclick事件中需要转义什么?

时间:2012-11-20 22:33:25

标签: php javascript onclick escaping

我不确定哪些字符需要在字符串中转义。我的PHP代码给出了字符串参数,但有时它有引号和双引号,我不知道该怎么做。

onclick="eventBox('This is the string ' // " "')"

2 个答案:

答案 0 :(得分:3)

引号(")应替换为"。反斜杠(\)前置于'

onclick="eventBox('This is the string \' // " "')"

答案 1 :(得分:1)

您需要转义引号或对其进行编码,以便正确解析。

onclick="eventBox('This is the string \' // " "')"

否则会因错误匹配引号而导致解析错误