简体   繁体   English

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

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

Per this similar post , I tried the following but none-worked, with error noted to side: 在这篇类似的文章中 ,我尝试了以下操作,但均无济于事,并指出了错误:

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)

Any ideas, other than putting the alert into a function? 除了将警报放入功能之外,还有其他想法吗? I imagine there is a way to do this in html/javascript? 我想有办法在html / javascript中做到这一点?

NOTE: I updated the title to qualify the question for using Grails. 注意:我更新标题以限定使用Grails的问题。 All the solutions below may well work in general, but not in grails 1.3.7. 以下所有解决方案通常都可以正常使用,但不适用于Grails 1.3.7。 What did work, per MaxArt's direction, is adding an additional backslash before each backslash, in version 4 above. 按照MaxArt的指示,有效的方法是在上述版本4中的每个反斜杠之前添加一个额外的反斜杠。

The last form is the correct one. 最后一种形式是正确的形式。 I don't know how Grails handles that, but you may try to escape the backslashes. 我不知道Grails是如何处理的,但是您可以尝试避免使用反斜杠。

You can always resolve to (and it's actually a better practice) attach an event listener to the element rather than using the traditional event declaration. 您始终可以决定(实际上是一种更好的做法)将事件侦听器附加到元素,而不是使用传统的事件声明。 This should be executed after the body has loaded: 这应该在主体加载后执行:

document.getElementById("alerter").addEventListener("click", function() {alert('This is my "quoted text"');}, false);

Of course, use attachEvent for Internet Explorer < 9. 当然,对于Internet Explorer <9,请使用attachEvent

And this should be your element: 这应该是您的要素:

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

Have you tried 你有没有尝试过

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

Just another option. 只是另一个选择。

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

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