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

发布于 2024-12-11 13:01:09 字数 744 浏览 1 评论 0原文

根据这个类似的帖子,我尝试了以下方法,但没有-有效,但有错误提示:

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 中,有效的方法是在每个反斜杠之前添加一个额外的反斜杠。

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?

NOTE: I updated the title to qualify the question for using Grails. All the solutions below may well work in general, but not in grails 1.3.7. What did work, per MaxArt's direction, is adding an additional backslash before each backslash, in version 4 above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-12-18 13:01:09

最后一种形式是正确的。我不知道 Grails 如何处理这个问题,但您可以尝试避开反斜杠。

您始终可以决定(这实际上是更好的做法)将事件侦听器附加到元素,而不是使用传统的事件声明。这应该在主体加载后执行:

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

当然,对于 Internet Explorer 使用 attachEvent attachEvent 9.

这应该是你的元素:

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

The last form is the correct one. I don't know how Grails handles that, but you may try to escape the backslashes.

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.

And this should be your element:

<span id="alerter">Quoted text</span>
情话已封尘 2024-12-18 13:01:09

您是否尝试过

“ for a left
” for a right

另一种选择。

Have you tried

“ for a left
” for a right

Just another option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文