简体   繁体   English

Hover 在表格的行上并在该行下方打开一行以获取更多详细信息

[英]Hover on a table's row and open a row below the row for more details

I have the following table:我有下表:

DEMO:演示:

<table class="table">
  <thead>
    <tr>
      <th>Airport</th>
      <th width="150px">Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td div class='action'>flight leaves on 13:20<BR>Take the train from ABC</td> 
      <td>JFK</td> 
      <td>234</td>      
    </tr>
    <tr>
      <td>LAX</td>
      <td>104</td>
    </tr>
  </tbody>
</table>

and css和 css

.action {
  display: none;
}

tr:hover .action {
  display: block;
}

Current result shows that when the user hover over the airport name the text appears inline:当前结果显示,当用户 hover 覆盖机场名称时,文本显示为内联: 在此处输入图像描述

My objective : when user hover over the airport name, he will see detail information in a line BELOW the airport and it should take the entire space.我的目标:当用户 hover 超过机场名称时,他会在机场下方的一行中看到详细信息,它应该占据整个空间。 Example, hover over JFK you will get:例如,通过 JFK 的 hover 您将获得:

Airport    Value
JFK        234

flight leaves on 13:20
Take the train from ABC

LAX        104

I was hoping that the display: block will do the trick but it comes to the left.我希望 display: block 能解决问题,但它在左边。

First of all, add a "+" in css as following given;首先,在 css 中添加一个“+”,如下所示;

.action {
  display: none;
}
tr:hover + .action {
  display: block;
}

Then, change your html codes like this;然后,像这样更改您的 html 代码;

<table class="table">
  <thead>
    <tr>
      <th>Airport</th>
      <th width="150px">Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>JFK</td> 
      <td>234</td>      
    </tr>
    <tr class="action">
      <td>flight leaves on 13:20 Take the train from ABC</td>
    </tr>
    <tr>
      <td>LAX</td>
      <td>104</td>
    </tr>
  </tbody>
</table>

You can try it on this demo .你可以在这个演示上试试。

When you use display: none/block the layout of the table will change, because when the element is not displayed, there is no space allocated for it between the other tags.当你使用display: none/block时,表格的布局会改变,因为当元素不显示时,其他标签之间没有为它分配空间。 You can use visibility: collapse instead.您可以改用visibility: collapse From the MDN docs来自MDN 文档

The collapse keyword has different effects for different elements: collapse关键字对不同的元素有不同的效果:

For <table> rows , columns, column groups, and row groups, the row(s) or column(s) are hidden and the space they would have occupied is removed (as if display: none were applied to the column/row of the table).对于<table>、列、列组和行组,行或列被隐藏,并且它们本来占用的空间被删除(就像 display: none 应用于列/行桌子)。 However, the size of other rows and columns is still calculated as though the cells in the collapsed row(s) or column(s) are present.但是,仍会计算其他行和列的大小,就好像折叠的行或列中的单元格存在一样。 This value allows for the fast removal of a row or column from a table without forcing the recalculation of widths and heights for the entire table.此值允许从表中快速删除行或列,而无需强制重新计算整个表的宽度和高度。

Notice the <td colspan="2"> so that the single td in the row spans over both columns注意<td colspan="2">以便行中的单个 td 跨越两列

 tr.action { visibility: collapse; } tr:hover + tr.action { visibility: visible; }
 <table class="table"> <thead> <tr> <th>Airport</th> <th width="150px">Value</th> </tr> </thead> <tbody> <tr> <td>JFK</td> <td>234</td> </tr> <tr class="action"> <td colspan="2">flight leaves on 13:20 Take the train from ABC</td> </tr> <tr> <td>LAX</td> <td>104</td> </tr> </tbody> </table>

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

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