如何使带有 Rails 链接的 Div 可点击? [英] How to make a Div with a Rails Link clickable?

查看:21
本文介绍了如何使带有 Rails 链接的 Div 可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 div:

.limeskin:hover {背景:#eee;光标:指针;显示:块;}

我想要可点击.因为我使用的是 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>价格:</b><%= number_to_currency(post.price, :unit => "R") %><br>[...]<%结束%>

任何简单的合法可行的答案?

谢谢

解决方案

link_to 可以接受一个块:

<%= link_to root_path do %><div>嘿!</div><%结束%>

这将用 标签包围 div.

文档:http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

或者,如果您有一个很大的 div 并且想要使其可点击",请使用 jQuery:

# html.erb<div class="limeskin"><div class="limebox"><div class="limecost"><b>价格:</b><%= number_to_currency(post.price, :unit => "R") %><br>#[...]

# jQuery.js$('.limeskin').click(函数(事件){var clicked_div = $(this);# 对事件对象和this"进行处理# 代表你刚刚点击的元素});

jsFiddle:http://jsfiddle.net/Lxw34w5o/1/

I have a large div:

.limeskin:hover {
  background: #eee;
  cursor: pointer;
  display: block;
}

that I want to be clickable. Because I'm using Rails I need to have a Rails link be clickable: For example

<%= link_to 'Edit Your Email Address', edit_user_path %>

I'm struggling to this.

Here is the whole block:

<% @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 %>

Any simple legal workable answers?

Thanks

解决方案

link_to can accept a block:

<%= link_to root_path do %>
  <div>Hey!</div>
<% end %>

This will surround the div with <a> tags.

Documentation: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Or if you have a big div and want to make it "clickable", using 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
});

jsFiddle: http://jsfiddle.net/Lxw34w5o/1/

这篇关于如何使带有 Rails 链接的 Div 可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆