简体   繁体   English

js字符串插值在haml中失败:javascript过滤器

[英]js string interpolation fails in haml :javascript filter

In a haml and jquery environment, the following haml fails:在 haml 和 jquery 环境中,以下 haml 失败:

#result

:javascript
  var foo = "result"
  $(`#${foo}`).text('some text')

Other tests show that string interpolation in a javascript filter like this:其他测试表明 javascript 过滤器中的字符串插值是这样的:

#result

:javascript
  var foo = "qux"
  $('#result').text(`#${foo}`)

fails when '$' is immediately preceded by '#'.当 '$' 前面紧跟 '#' 时失败。 But it succeeds when there is a whitespace between!但是当中间有空格时它会成功!

How can I implement string interpolation within the javascript filter when I need '#' to precede the embedded value?当我需要在嵌入值之前使用“#”时,如何在 javascript 过滤器中实现字符串插值?

thanks in advance for any insight you can offer.提前感谢您提供的任何见解。

[Edit] of course @Barmar's concatenation suggestion is a good workaround. [编辑] 当然@Barmar 的串联建议是一个很好的解决方法。 I also just found that this is reported as issue# 975 in the haml git repo我也刚刚发现这在haml git repo中被报告为问题#975

Try just concatenating the string, as # is special in HAML.尝试只连接字符串,因为#在 HAML 中是特殊的。 $('#' + foo)

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

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