简体   繁体   English

图片上 <tr> 将鼠标悬停在删除行上

[英]Image on <tr> hover to delete row

Goal : Hovering over table row <tr> causes a button to become visible to the right of the <tr> element (outside of the table). 目标 :将鼠标悬停在表格行<tr>会导致按钮在<tr>元素右侧(表格外部)可见。 Clicking this button deletes the row. 单击此按钮可删除该行。

I'm struggling with the CSS and the mechanics of showing the button. 我正在努力使用CSS和显示按钮的机制。 I find I am able to create the desired button hide/show behavior on a <div> tag, but I can't figure out how to implement it within a table. 我发现我能够在<div>标签上创建所需的按钮隐藏/显示行为,但我无法弄清楚如何在表格中实现它。

See fiddle here . 请看这里的小提琴

The actual deleting of the row is fairly straightforward to me... 实际删除行对我来说相当简单......

$('table tr input').click(function(e) {
  var answer = confirm("Delete row?")
  if (answer) {

   var ind=$(this).closest('td').parent()[0].sectionRowIndex;
   $("tr:eq("+ind+")").remove();

  } else {
    //some code
  }

});

Regarding the "outside of the table" requirement, instead of matching up absolute/relative positions to row positions, you can try out putting the button in a 4th column. 关于“表外”的要求,您可以尝试将按钮放在第4列中,而不是将绝对/相对位置与行位置匹配。 And instead of applying the border to the table, apply it to TDs, and turn off the border or the 4th column. 而不是将边框应用于表格,将其应用于TD,并关闭边框或第4列。 This will keep your button aligned with the rows but appear as if it is outside of the table. 这将使您的按钮与行对齐,但看起来好像它在表格之外。

https://jsfiddle.net/sg2y9fya/ https://jsfiddle.net/sg2y9fya/

 .show-image td { border: solid 1px black; } .show-image th { border-top: solid 1px black; } .show-image th:first-child { border-left: solid 1px black; } .show-image th:last-child { border-right: 1px solid black; } .show-image td:last-child { border: 0; } .show-image td:last-child input { display:none; } .show-image tr:hover > td:last-child > input { display:block; } table.show-image { table-padding: 0; table-spacing: 0; border-collapse: collapse; } 
 <table class="show-image"> <tr> <th>header1</th> <th>header2</th> <th>header3</th> </tr> <tr> <td>data1</td> <td>data2</td> <td>data3</td> <td><input class="row-del-btn" type="button" value=" Delete " /></td> </tr> <tr> <td>data4</td> <td>data5</td> <td>data6</td> <td><input class="row-del-btn" type="button" value=" Delete " /></td> </tr> <tr> <td>data7</td> <td>data8</td> <td>data9</td> <td><input class="row-del-btn" type="button" value=" Delete " /></td> </tr> </table> 

That's easy, just two CSS rules: 这很容易,只有两个CSS规则:

 $('table td:last-child button').click(function(e) { var answer = confirm("Delete row?") if (answer) { var $row = $(this).closest('tr'); $row.remove(); } else { //some code } }); 
 tr > td:last-child > button {visibility:hidden;} tr:hover > td:last-child > button {visibility:visible;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="show-image"> <tr> <th>header1</th> <th>header2</th> <th>header3</th> </tr> <tr> <td>data1</td> <td>data2</td> <td>data3<button>x</button></td> </tr> <tr> <td>data4</td> <td>data5</td> <td>data6<button>x</button></td> </tr> <tr> <td>data7</td> <td>data8</td> <td>data9<button>x</button></td> </tr> </table> 

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

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