简体   繁体   中英

Templating and escaping with Coffeescript/JQuery

This below selector in some coffeescript that I wrote keeps giving me trouble.

$('span[data-id="#{cat_id}"]').attr('class', 'dropdown-toggle icon-button fold')

When compiled and run, the text inside the quotes becomes:

span[data-id="#{cat_id}"]

I tried changing the single quotes on the outside of the selector to double quotes, but then I get a runtime error:

ExecJS::RuntimeError in Application#handle_404 Showing /Users/michaeldiscenza/Documents/RUN_source_repos/run_portal/app/views/layouts/application.html.erb where line #33 raised: [stdin]:222:88: error: unmatched OUTDENT $('span[data-id="#{cat_id}"]').children().attr('class', 'icon toggle collapse')

I've tried concatenating "/'" + selector + "/'" , but that didn't work either.

$("span[data-id=#{cat_id}]")

我猜想在内部的引号是不必要的,但是在外部仍然需要用双引号来解释#{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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