根据这个similar post ,我尝试了以下方法,但没有成功,并在旁边注明了错误:
onclick="alert('This is my \"quoted text:\"');" (no browser response)
onclick="alert('This is my \\"quoted text:\\"'); (no browser response)
onclick="alert('This is my "quoted text:"');" (comes out as: This is my "quoted text:")
onclick="alert('This is my \x22quoted text:\x22');" (grails .gsp page preprocessor chokes on this)
除了将警报放入函数之外,还有什么想法吗?我想有办法在 html/javascript 中做到这一点吗?
注意:我更新了标题以限定使用 Grails 的问题。下面的所有解决方案通常都可以正常工作,但不适用于 grails 1.3.7。根据 MaxArt 的指示,在上面的版本 4 中,有效的方法是在每个反斜杠之前添加一个额外的反斜杠。
最佳答案
最后一种形式是正确的。我不知道 Grails 如何处理这个问题,但您可以尝试转义反斜杠。
您始终可以决定(这实际上是更好的做法)将事件监听器附加到元素,而不是使用传统的事件声明。这应该在正文加载后执行:
document.getElementById("alerter").addEventListener("click", function() {alert('This is my "quoted text"');}, false);
当然,对于 Internet Explorer <9,请使用 attachEvent
。
这应该是你的元素:
<span id="alerter">Quoted text</span>
关于javascript - 使用 Grails 在执行警报的事件处理程序中转义双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7863497/