简体   繁体   English

使Div标签可点击

[英]Make Div tag clickable

Hi Below code gives me the list of messages in table row format and i want to make each row as clickable. 嗨,下面的代码为我提供了表格行格式的消息列表,我希望每一行都可单击。 I have tried every suggestion in SO but could not make it work. 我已经尝试了SO中的所有建议,但未能使其奏效。
As i could not make whole tr clickable, i made i.reason field as clickable as temprorary solution. 由于我无法使整个tr i.reason单击,因此我将i.reason字段设置为可作为临时解决方案单击的字段。

<% if @notifyCount > 0 %>
<table class="table table-hover table-condensed">
<% @notify.each do |i| %>
<tr class="notifyTable" id= "<%=i.id %>">
 <td class="myWidth concat"> 
    <%= image_tag "#{i.incident_image}", style: "height:45px; width:45px; float:left; margin: 0 8px 0 0" %> <b> <%= i.sender.first_name + ' ' + i.sender.last_name %> </b> <br> 
    <div> <%= link_to truncate(i.reason.capitalize, length: 50), message_member_path(id: i.id), style: "text-decoration:none" %> </div>
 </td>
</tr>
<%end%>
</table>
<%else%>
<p style="opacity:0.7;text-align:center;margin-top:5px"> No Unread Messages ...</p>
<%end%>

JS JS

You'll have to use Javascript to make this work 您必须使用Javascript才能完成这项工作

Javascript (and JQuery) basically works on the front-end of the browser (inside the DOM - Document Object Model ) to bind events to elements on the page. Javascript(和JQuery)基本上在浏览器的前端(在DOM - Document Object Model内 )工作,以将事件绑定到页面上的元素。

Here's how you'd made a div "clickable" using JQuery: 使用JQuery将div设为“可点击”的方法如下:

#app/assets/javascripts/application.js
$(document).on("click", ".div", function() {
   // Your Action Here
});

-- -

In regards to the specifics of your question, you may need to look at using the following code: 关于问题的细节,您可能需要使用以下代码进行查看:

#app/assets/javascripts/application.js
$(document).on("click", ".notifyTable", function(){
   alert("Row is clicked");
});

This will allow you to capture the click event. 这将允许您捕获单击事件。 If you want to then perform things with that click, you'll be best adding data attributes to your row , like so: 如果您想通过单击来执行操作,则最好将data属性添加到row ,如下所示:

<tr class="notifyTable" id= "<%=i.id %>" data-link="http://google.com">

This would allow you to do this: 这将允许您执行以下操作:

#app/assets/javascripts/application.js
$(document).on("click", ".notifyTable", function(){
   window.location.href = $(this).data("link")
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM