问题描述
我目前正在尝试向Rails image_tag添加自定义数据属性,以允许我的jQuery运行.
I am currently trying to add a custom data attribute to a Rails image_tag to allow my jQuery to function.
首先,我从像这样的image_tag开始:
First, I am starting with an image_tag like so:
<%= image_tag("randomv1.png", class: "icon js-icon") %>
第二,对于我的jQuery,我需要image_tag来触发一个事件,并且我希望它包含一个数据属性,例如我在此处用于按钮的属性:
Second, for my jQuery, I need the image_tag to trigger an event, and I want it to include a data attribute like the one I am using for a button here:
<button class="icon js-icon" data-icon-text="Random">Icon graphic</button>
添加class属性很容易,但是我也需要data-icon-text作为属性,以便在.hover之后显示数据.
Adding the class attribute is easy, but I will need the data-icon-text to function as an attribute too for the data to be displayed after a .hover.
基于这个先前的问题,我试图修改image_tag:
Based on this previous question, I tried to modify the image_tag:
<%= image_tag("randomv1.png", :class "icon js-icon" :data => { :icon-text => "Random" }) %>
由于这传递了错误,我可以创建该自定义属性吗?
Since this is delivering an error, can I create that custom attribute?
推荐答案
尝试执行以下操作:
您必须修改需要=>
的课程.然后,您必须将:icon-text
更改为字符串"icon-text"
.
You have to revised the class need =>
. Then, you have to change :icon-text
into string "icon-text"
.
<%= image_tag("randomv1.png", :class => "icon js-icon", :data => { "icon-text" => "Random" }) %>
或
<%= image_tag("randomv1.png", :class => "icon js-icon", "data-icon-text" => "Random") %>
希望对您有帮助
这篇关于在Rails image_tag中创建自定义数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!