首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用HAML有条件地设置HTML元素id

使用HAML有条件地设置HTML元素id
EN

Stack Overflow用户
提问于 2010-10-02 00:23:50
回答 3查看 15.2K关注 0票数 20

我正在尝试用haml编写此html,以便如果该项目是当前项目,则添加id标签。这是为了设置jquery高亮显示。

代码语言:javascript
复制
<% if line_item == @current_item %>
<tr class="line_item id="current_item">
<% else %>
<tr class="line_item">
<% end %> 
  <td><%= line_item.quantity %>&times;</td>
  <td><%= line_item.product.title %></td> 
  <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>

因为我不想编写helper方法,所以我将if语句放在了标签中:

代码语言:javascript
复制
%tr.line_item{ :id => (line_item == @current_item ? '' : 'current_item') }
%td
    = line_item.quantity
%td 
    \x #{line_item.product.title}
%td.item_price
    = number_to_currency(line_item.total_price)
%td.item_remove
    = button_to 'Remove', line_item, :method => :delete

然而,'current_item‘的id标签适用于所有的项目,而不仅仅是当前的项目。这会导致javascript高亮显示所有条目或错误条目。关于如何让haml合作的想法?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3841116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档