使用Grails在发出警报的事件处理程序中转义双引号

Escaping double quotes in event handler that does an alert, using Grails

本文关键字:程序 转义 事件处理 Grails 使用      更新时间:2023-09-26

对于这个类似的帖子,我尝试了以下操作,但没有成功,并在旁边注明了错误:

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使用attachEvent <9 .

这应该是你的元素:

<span id="alerter">Quoted text</span>

你试过了吗

&#8220; for a left
&#8221; for a right

只是另一个选项