我有一个div,其类在我的视图中定义如下
<div class=<%= "validation-#{index} questions col-lg-8 col-lg-offset-2 col-md-9 col-md-offset-1 col-sm-9 col-sm-offset-1 col-xs-11 col-xs-offset-1" %>>
但是字符串插值在视图中输出效果不佳
<div class="validation-1" questions="" col-lg-8="" col-lg-offset-2="" col-md-9="" col-md-offset-1="" col-sm-9="" col-sm-offset-1="" col-xs-11="" col-xs-offset-1="">
任何人都可以告诉错误吗?
答案 0 :(得分:2)
改变这个:
<div class=<%= "validation-#{index} questions col-lg-8 col-lg-offset-2 col-md-9 col-md-offset-1 col-sm-9 col-sm-offset-1 col-xs-11 col-xs-offset-1" %>>
到此:
<div class='<%= "validation-#{index} questions col-lg-8 col-lg-offset-2 col-md-9 col-md-offset-1 col-sm-9 col-sm-offset-1 col-xs-11 col-xs-offset-1" %>'>
如果您将代码更改为此内容,它会更清晰,更少混淆:
<div class='questions col-lg-8 col-lg-offset-2 col-md-9 col-md-offset-1 col-sm-9 col-sm-offset-1 col-xs-11 col-xs-offset-1 <%= "validation-#{index}" %>'>
问题是你的ERB代码被评估并以<div class= validation-1 questions col-lg-8 col-lg-offset-2 col-md-9 col-md-offset-1 col-sm-9 col-sm-offset-1 col-xs-11 col-xs-offset-1 >
的形式呈现给HTML,但由于浏览器足够聪明以纠正错误,它需要第一个字符串并将其分配给类:class= "validation-1 "
并且休息将被转换为属性和价值(即#34;&#34;)紧接着另一个。