我在处理以下 html 时遇到问题
<span onClick="alert('hi" more stuff')">Works</span><br/>
<span onClick='alert("Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.")'>Works long text</span>
<br />
<span onClick='alert("Far far " away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.")'> Doesn't work long text</span>
我的问题是,在第三个 onclick 处理程序中,我在浏览器中收到未终止的字符串错误。在实际程序中,参数字符串是在客户端生成的,因此所有编码都必须在那里发生。
在有效的示例中,我尝试了 "
和 "
,并且在最后一个示例中有效。但它们在第一个示例中确实有效
最佳答案
要在 JavaScript "
分隔字符串中使用 "
,请使用 \"
。
要在 HTML "
分隔属性值中使用 "
,请使用 "
。
如果需要的话将它们组合起来:\"
或者,停止使用内部事件属性,写入 unobtrusive JavaScript并绑定(bind)您的事件处理程序 programatically 。
关于javascript - 在 JavaScript 函数参数中传递双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167712/