[英]How to make a Div with a Rails Link clickable?
我有一个很大的 div:
.limeskin:hover {
background: #eee;
cursor: pointer;
display: block;
}
我想要可点击。 因为我使用的是 Rails,所以我需要有一个可点击的 Rails 链接:例如
<%= link_to 'Edit Your Email Address', edit_user_path %>
我正在为此而挣扎。
这是整个块:
<% @user.posts.each do |post| %>
<div class="lists">
<ol class="limeposts">
<li>
<div class="limeskin">
<div class="limebox">
<div class="limecost">
<b>Price:</b>
<%= number_to_currency(post.price, :unit => "R") %><br>
[...]
<% end %>
任何简单的合法可行的答案?
谢谢
link_to
可以接受一个块:
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
这将用<a>
标签包围 div。
文档: http : //apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
或者,如果您有一个很大的 div 并且想要使其“可点击”,请使用 jQuery:
# html.erb
<div class="limeskin">
<div class="limebox">
<div class="limecost">
<b>Price:</b>
<%= number_to_currency(post.price, :unit => "R") %><br>
#[...]
</div>
</div>
</div>
# jQuery.js
$('.limeskin').click( function(event) {
var clicked_div = $(this);
# do stuff with the event object and 'this' which
# represent the element you just clicked on
});
使用 javascript(我推荐 jQuery)使动作真正发生,使用 CSS hover
选择器修改 div 背景和光标(将光标从箭头更改为手形)。
即使您有一个包含多个标签的大块,使用下面的 Link_to 就足够了:
<%= link_to desired_path do %>
<div class="linkable">
<another div>
... some other tags
</another div>
</div>
<% end %>
我建议您为鼠标悬停事件使用不同的背景颜色,因为它向查看器显示它是一个链接!
在你的 .css 文件中:
.linkable:hover{
background-color: red;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.