【问题标题】:What's the easiest way to increment an ID in HAML?在 HAML 中增加 ID 的最简单方法是什么?
【发布时间】:2013-10-08 01:13:28
【问题描述】:
    -@things.each do |thing|
      %div.thing
        = thing

我见过几种不同的技术。但是使用上面的 HAML 模板,我希望每个 thing 都有一个从 1 到 n+1 的唯一 ID。为所有 things 动态增加 id 的最佳方法是什么?

例如:对于 n 事物,我希望 HTML 看起来像这样。

<div class='thing' id='1'>
<div class='thing' id='2'>
...
<div class='thing' id=n+1>

【问题讨论】:

  • ID 不能以整数开头,它是无效的。
  • 您可以在末尾添加递增的 id 编号,只需给它一个以 thing 开头的 id,然后您可以执行 thing1 thing2 thing3
  • @PatrickEvans 可以。但是你确定ID的事情吗?我正在加载一个页面来测试它,这很好
  • @KeithJohnson 请参阅this question 了解可接受的 ID。
  • @PatrickEvans 好的链接谢谢!

标签: html ruby-on-rails haml


【解决方案1】:
-@things.each_with_index do |thing,index|
  %div.thing{:id => "thing#{index}"}
    = thing

【讨论】:

  • 是的,无论谁投反对票,都需要解释。我认为这个解决方案没有任何问题。
【解决方案2】:

dom_id 之类的内容可能会帮助您生成 HTML 元素的唯一 ID。它还将解决以数字开头的问题。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签