简体   繁体   English

如何逃避javascript的“\\”

[英]How to escape a javascript 's “\”

Inside a render function in my template I'm passing this parameter: 在我的模板中的渲染函数内部,我传递了这个参数:

placeholderTitle: (gettext "Companie\'s name")

I need to escape the ' because that string is going to be used inside inline javascript: 我需要转义'因为该字符串将在内联javascript中使用:

onfocus="if (this.textContent.trim() == 'Companie's name') {//do someting}"

But like that is not working. 但是那样不起作用。

You should use Poison.encode! 你应该使用Poison.encode! to encode variables that are to be printed in JS code. 编码要在JS代码中打印的变量。 The following should work if you pass this as an attribute to HTML helpers like content_tag : 如果您将此属性作为属性传递给content_tag等HTML帮助程序,则以下内容应该有效:

onfocus: "if (this.textContent.trim() == #{Poison.encode!(gettext("Companie's name"))} {//do someting}"

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

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