【问题标题】:HAML: Create data-xxx-yyy attributeHAML:创建 data-xxx-yyy 属性
【发布时间】:2012-09-09 22:13:29
【问题描述】:

我知道如何使用 data-* 属性创建链接:

%a{ :href => "#", :data => { :name ="John", :age => 24 } } Hi John

生成:

<a href="#" data-name="John" data-age="24">Hi John</a>

但是如何使用 2 个深度数据名称,例如 data-user-namedata-user-age,这是一种对数据属性进行分组的方法。你可能猜到了,我试过了:

:data => { :user => { :name => "John", :age => 24 } }

但它不起作用,给我奇怪的 HTML 输出:

<a href="#" data-user="nameJohnage24">Hi John</a>

知道怎么做吗?提前致谢。

【问题讨论】:

    标签: ruby-on-rails html haml


    【解决方案1】:

    你必须使用

    :data => {'user-name' => 'John', 'user-age' => 24}
    

    data 属性是 HAML 的特例,它只考虑浅值。

    【讨论】:

    • 所以,不能像我们对 data- 的分组方式那样对 user- 分组?
    • 不,因为 data 属性是 HAML 的特殊情况,它只考虑浅值。你可以在github.com/haml/haml 上打开一个问题来引发一些讨论。我没看,但如果之前提到过,我不会感到惊讶。
    【解决方案2】:

    是的,你可以!从简单...

    %a(data-user-name="John", data-user-last-name="Arbuckle")
    

    到复合体

    %a(data-user-name="#{User.first.name}", data-stack-overflow="all of these will be custom attributes in your link"){href: "garfield.com"}
    

    【讨论】:

      猜你喜欢
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 2023-03-22
      • 1970-01-01
      • 2019-06-14
      • 2014-05-25
      • 1970-01-01
      相关资源
      最近更新 更多