简体   繁体   English

怎么做 <tr> 可点击作为链接而不是单元格?

[英]How to make <tr> clickable as a link instead of cell?

This must be a very easy and simple but i"m finding it hard to get it done. 这必须是非常容易和简单的,但是我发现很难完成它。

I have a list of rows with 6 Column which shows data from mysql. 我有一个带有6列的行列表,其中显示了来自mysql的数据。 I would like to make each row clickable instead of cell which opens a new url in new tab. 我想使每一行都可以单击,而不是单击可在新标签中打开新网址的单元格。

Here is the structure. 这是结构。

<tr> <a target="_" href="https://www.google.com">



<td style="text-decoration: underline; font-size: 15px;">  </td>
<td><?php echo $row['district']; ?></td>
<td><?php echo $row['program']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['yoa']; ?></td>
<td><?php echo $row['email']; ?></td>


</a></tr>

Above Method Don't work. 上面的方法不起作用。 Any Help is appreciated 任何帮助表示赞赏

Try this (without JS): 试试这个(没有JS):

  <tr> <td style="text-decoration: underline; font-size: 15px;"></td> <td> <a href="http://google.com"> <div style="height:100%;width:100%"> 1<?php echo $row['district']; ?> </div> </a> </td> <td> <a href="http://google.com"> <div style="height:100%;width:100%"> 2<?php echo $row['program']; ?></td> </div> </a> <td> <a href="http://google.com"> <div style="height:100%;width:100%"> 3<?php echo $row['gender']; ?> </div> </a> </td> <td> <a href="http://google.com"> <div style="height:100%;width:100%"> 4<?php echo $row['yoa']; ?> </div> </a> </td> <td> <a href="http://google.com"> <div style="height:100%;width:100%"> 5<?php echo $row['email']; ?> </div> </a> </td> 

Remove tag "A" 删除标签“ A”

and bind onClick event at "TR" 并在“ TR”处绑定onClick事件

<tr onclick="_linkWhatYouWant" style="cursor: pointer;"> <td> ... blabla </td> </tr>

Try this: 尝试这个:

tr {
    position: relative;
}

tr a {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

This will make anchor tag occupies the whole tr width and height, if needed, add z-index to <a> 这将使定位标记占据整个tr宽度和高度,如果需要,请在<a>添加z-index

I would like to say that this is not correct by the semantic point of view; 我想说,从语义的角度来看这是不正确的。 However, you can set the role=button rule in your html. 但是,您可以在html中设置role=button规则。

 function navigateTo(url) { window.location.href(url); } document.querySelector('#row-1').addEventListener('click', function() { navigateTo('http://www.google.com/') }); 
 table { border-collapse: collapse; } tr { border: 1px solid black; cursor: pointer; } tr:hover { background: rgba(255,0,0,0.3); } 
 <table> <tr id='row-1' role='button'> <td>Item 1</td> <td>Item 2</td> <td>Item 3</td> <td>Item 4</td> <td>Item 5</td> <td>Item 6</td> </tr> <tr id='row-2' role='button'> <td>Item 7</td> <td>Item 8</td> <td>Item 9</td> <td>Item 10</td> <td>Item 11</td> <td>Item 12</td> </tr> </table> 

You can use jQuery and click function. 您可以使用jQuery并单击功能。 This approach is simple and it gives you freedom to decide which rows are used as links. 这种方法很简单,它使您可以自由决定将哪些行用作链接。 All you need to do is add a class and data attribute. 您需要做的就是添加一个class和data属性。 You can also add cursor:pointer property in CSS. 您还可以在CSS中添加cursor:pointer属性。

 $('.click').click(function(){ window.location = $(this).data('href'); //use window.open if you want a link to open in a new window }); 
 .click{cursor:pointer} 
 <tbody> <tr class="click" data-href="some-url-here"> <th>1</th> <td>John Smith</td> <td>1-234-567-8900</td> </tr> </tbody> 

Consider the following example: 考虑以下示例:

https://jsfiddle.net/Twisty/3hnt6mws/ https://jsfiddle.net/Twisty/3hnt6mws/

HTML 的HTML

<table>
  <tr data-link="https://www.google.com" data-target="_BLANK">
    <td>
      District
    </td>
    <td>
      Program
    </td>
    <td>
      Gender
    </td>
    <td>
      YOA
    </td>
    <td>
      Email
    </td>
  </tr>
</table>

JavaScript 的JavaScript

$(function() {
  $("table tr").click(function(e) {
    var u = $(this).data("link");
    var t = $(this).data("target");
    console.log(u, t);
    if (t.length) {
      window.open(u, t);
    } else {
      window.location.href = u;
    }
  });
});

Using the data attribute, you can store more info with the row and use that in the click event. 使用data属性,您可以在行中存储更多信息,并在click事件中使用它。 You can then programmatically direct the browser to open a new window or direct to a new location. 然后,您可以以编程方式将浏览器定向为打开新窗口或定向至新位置。

Hope that helps. 希望能有所帮助。

The <tr> tag defines a row in an HTML table.

A <tr> element contains one or more <th> or <td> elements.

<table>
  <a href="www.google.com"><tr>
    <th>Month</th>
    <th>Savings</th>
  </tr></a>
  <tr>
</table>

try this will help you u can use <button> tag also to display like a button

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

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