使用 Underscore.js 在 html 属性中插入 [英] Interpolate inside html attributes with Underscore.js

查看:29
本文介绍了使用 Underscore.js 在 html 属性中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Backbone.js、Underscore.js、HAML 和 Coffeescript 构建应用程序.

I'm building an application using Backbone.js, Underscore.js, HAML, and Coffeescript.

我遇到的问题是让变量插入到 html 元素属性中.

The problem I'm having is getting variables to interpolate inside of html element attributes.

<% _.each(collection.models, function(document) { %>
%tr
  %td
    %input{:type => 'checkbox', :name => "documents[]", :value => "<%= document.attributes.id %>"}
  %td <%= document.attributes.id %>
  %td <%= document.attributes.name %>

  <% } %>
<% }); %>

对象的值在 <td> 内正确显示,但不在输入的 value 属性内.

The object's values are displaying properly inside of the <td>, but not within the input's value attribute.

是否可以在元素的属性内进行插值?我找不到解决方案.

Is interpolation inside of an element's attributes possible? I was not able to find a solution.

谢谢

推荐答案

解决这个问题的方法是使用 HAML 的 :escape_attrs 选项.

The solution to this problem is to use HAML's :escape_attrs option.

Haml::Engine.new(template, :escape_attrs => false).render

这篇关于使用 Underscore.js 在 html 属性中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆