jquery - 悬停时仅显示表格一行的隐藏元素

标签 jquery html css

过去一个小时我一直在尝试解决这个问题,但没有成功。也许你们中的一些人可以提供帮助。

我想要做的就是让 glyphicon 在表行悬停时可见,但仅限于那个特定的悬停行。它现在所做的只是将鼠标悬停在任何行上,将使所有隐藏的字形图标可见。

我创建了一个 JSFiddle重现问题,但不知何故它无法正常工作,但你应该明白这一点。

我已经尝试对几乎所有内容使用 :first-child ,但这似乎没有帮助。

jQuery:

$(document).ready(function(){
    $(".table-hover tbody tr td").hover(function() {
        $(".glyphicon.glyphicon-triangle-right:first-child").css("visibility", "visible");
    },
    function () {
        $(".glyphicon.glyphicon-triangle-right:first-child").css("visibility", "hidden");
    });
});

HTML:

<div class="container-fluid text-center">
    <table class="table table-hover">
        <tr>
            <td><a href="#"><span class="glyphicon glyphicon-triangle-right pull-left"></span><span class="label label-info">bla bla</span>SOME TEXT HERE</a></td>
        </tr>
        <tr>
            <td><a href="#"><span class="glyphicon glyphicon-triangle-right pull-left"></span><span class="label label-info">bla bla</span>SOME TEXT HERE</a></td>
        </tr>
        <tr>
            <td><a href="#"><span class="glyphicon glyphicon-triangle-right pull-left"></span><span class="label label-info">bla bla</span>SOME TEXT HERE</a></td>
        </tr>
    </table>
</div>

CSS:

.glyphicon.glyphicon-triangle-right {
  visibility: hidden;
  margin-left: -4px;
  font-size: 20px;
}
.table a {
  display: block;
  text-decoration: none;
  color: #00b6ff;
}
.table a:hover {
  padding-left: -10px;
}
.table-hover tbody tr td, .table-hover tbody tr th {
  transition: 0.3s ease-in-out;
  -webkit-transition: 0.3s ease-in-out;
  -o-transition: 0.3s ease-in-out;
  -ms-transition: 0.3s ease-in-out;
  font-size: 17px;
  padding: 15px 0;
}
.table-hover tbody tr:hover td {
  background-color: #d1d1d1;
}

最佳答案

不使用 :first-child,而是像这样使用 jQuery 的 find():

$(document).ready(function() {
  $(".table-hover tbody tr td").hover(function() {
      $(this).find(".glyphicon.glyphicon-arrow-right").css("visibility", "visible");
    },
    function() {
      $(".glyphicon.glyphicon-arrow-right").css("visibility", "hidden");
    });
});
/* Latest compiled and minified CSS included as External Resource*/


/* Optional theme */


body {
  margin: 10px;
}

.label.label-info {
  margin-right: 10px;
  vertical-align: middle;
}

.glyphicon.glyphicon-arrow-right {
  visibility: hidden;
  margin-left: -4px;
  font-size: 20px;
}

.table a {
  text-decoration: none;
  color: #00b6ff;
}

.table-hover tbody tr td,
.table-hover tbody tr th {
  transition: 0.3s ease-in-out;
  -webkit-transition: 0.3s ease-in-out;
  -o-transition: 0.3s ease-in-out;
  -ms-transition: 0.3s ease-in-out;
  font-size: 17px;
  padding: 15px 0;
}

.table-hover tbody tr:hover td {
  background-color: #d1d1d1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="container-fluid text-center">
    <table class="table table-hover">
      <tr>
        <td><a href="#"><span class="glyphicon glyphicon-arrow-right pull-left"></span><span class="label label-info" style="margin-right: 10px; vertical-align: middle;"></span>hhhhhh</a></td>
      </tr>
      <tr>
        <td><a href="#"><span class="glyphicon glyphicon-arrow-right pull-left"></span><span class="label label-info">Registracija atidaryta!</span>2017.09.01 Rudens pradžios anglų kalbos kursai. Plačiau...</a></td>
      </tr>
      <tr>
        <td><a href="#"><span class="glyphicon glyphicon-arrow-right pull-left"></span>2017.09.01 Rudens pradžios anglų kalbos kursai. Plačiau...</a></td>
      </tr>
      <tr>
        <td><a href="#"><span class="glyphicon glyphicon-arrow-right pull-left"></span>2017.09.01 Rudens pradžios anglų kalbos kursai. Plačiau...</a></td>
      </tr>
      <tr>
        <td><a href="#"><span class="glyphicon glyphicon-arrow-right pull-left"></span>2017.09.01 Rudens pradžios anglų kalbos kursai. Plačiau...</a></td>
      </tr>
    </table>
  </div>

关于jquery - 悬停时仅显示表格一行的隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812158/

相关文章:

javascript - 将选择的(单击的)选项传递到输入框

javascript - 我无法获取数组 json 响应

javascript - 使用 "<select></select>"表单调整 html5 Canvas 的大小

html - 在 WebForms 下拉菜单中缩短选项

html - 表格单元格的高度和宽度完全不变

jquery - 如何在Bootstrap中使用AJAX发送表单数据?

javascript - 限制特定列的单击事件

javascript - jQuery load(),加载不正确。我该如何解决这个问题?

html - 使用带有 float 列表的 CSS 打印标签

css - 是否可以使列布局中的所有 flex 元素具有相同的宽度?