将鼠标悬停在动态添加的表行上

马切洛

我在表单元格上有一个带有悬停事件的表,该事件在标题单元格中切换了一个CSS类。如果我在表中动态添加一行,则该事件不会委托给新添加的行。我读到如果我使用.on()jquery方法,那么它应该委托给动态添加的内容。但事实并非如此。

var table = $("table tr");

var cells = $("tr");
var headerCells = $(".col");

cells.on({
    mouseenter: function () {
        var cellIndex = $(this).index();
        headerCells.eq(cellIndex).addClass("column-hover");
    },
    mouseleave: function () {
        var cellIndex = $(this).index();
        headerCells.eq(cellIndex).removeClass("column-hover");
    }
}, '.data');

var html = '<tr><td class="item">added row</td><td class="item">added row</td><td class="item">added row</td><td class="item">added row</td><td class="item">added row</td></tr>';

$(".add").click(function() {
  cells.last().after(html);
});
table {
  border-collapse: collapse;
}

td {
  border: 1px solid black;
}

.column-hover {
    color: white;
    background: #2196F3;
    z-index: 201;
    font-weight: 500;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="add">add item</button>
<table>
  <thead>
    <tr>
      <th class="col">header 1</th>
      <th class="col">header 2</th>
      <th class="col">header 3</th>
      <th class="col">header 4</th>
      <th class="col">header 5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="data">row 1, cell 1</td>
      <td class="data">row 1, cell 2</td>
      <td class="data">row 1, cell 3</td>
      <td class="data">row 1, cell 4</td>
      <td class="data">row 1, cell 5</td>
    </tr>
    <tr>
      <td class="data">row 2, cell 1</td>
      <td class="data">row 2, cell 2</td>
      <td class="data">row 2, cell 3</td>
      <td class="data">row 2, cell 4</td>
      <td class="data">row 2, cell 5</td>
    </tr>
    <tr>
      <td class="data">row 3, cell 1</td>
      <td class="data">row 3, cell 2</td>
      <td class="data">row 3, cell 3</td>
      <td class="data">row 3, cell 4</td>
      <td class="data">row 3, cell 5</td>
    </tr>
    <tr>
      <td class="data">row 4, cell 1</td>
      <td class="data">row 4, cell 2</td>
      <td class="data">row 4, cell 3</td>
      <td class="data">row 4, cell 4</td>
      <td class="data">row 4, cell 5</td>
    </tr>
    <tr>
      <td class="data">row 5, cell 1</td>
      <td class="data">row 5, cell 2</td>
      <td class="data">row 5, cell 3</td>
      <td class="data">row 5, cell 4</td>
      <td class="data">row 5, cell 5</td>
    </tr>
  </tbody>
</table>

我想念什么?

我在这里有一个小提琴:http : //jsfiddle.net/Ltcppvpy/

亚历克斯

附加的html(变量html)没有类,.data因此首先您应该将其添加到事件选择器列表('.data, .item')中,并且cells在向其添加元素时不进行更新,因此我的建议是使用$('body')而不是cellscells.on-> $('body').on)。

使用以下代码:Jsfiddle

$('body').on({
    mouseenter: function () {
        var cellIndex = $(this).index();
        headerCells.eq(cellIndex).addClass("column-hover");
    },
    mouseleave: function () {
        var cellIndex = $(this).index();
        headerCells.eq(cellIndex).removeClass("column-hover");
    }
}, '.data, .item');

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将鼠标悬停在动态添加的表行上

来自分类Dev

Javascript将鼠标悬停在元素的动态数量上

来自分类Dev

jQuery将鼠标悬停在动态对象上

来自分类Dev

将鼠标悬停在动态创建的列表上

来自分类Dev

单击表,更新行,将鼠标悬停在行上,更新表

来自分类Dev

将鼠标悬停在表的偶数行上时,框阴影不可见

来自分类Dev

将鼠标悬停在jQuery上

来自分类Dev

将鼠标悬停在SVG上

来自分类Dev

将鼠标悬停在div上

来自分类Dev

将鼠标悬停在元素上?

来自分类Dev

将鼠标悬停在div上,同时将鼠标悬停在整个标签上

来自分类Dev

将鼠标悬停在另一行/段上时,如何更改表的一行/段?

来自分类Dev

如何将鼠标悬停在两个不同表的同一行上?

来自分类Dev

将鼠标悬停在动态创建的图像上的appy fancybox

来自分类Dev

鼠标悬停在图像上添加文字

来自分类Dev

vuejs 鼠标悬停在此元素上添加类

来自分类Dev

将鼠标悬停在文本上时将鼠标悬停

来自分类Dev

将鼠标悬停在子div上会在父div上添加阴影

来自分类Dev

将鼠标悬停在HTML表格行上显示透明框(不突出显示行)

来自分类Dev

将鼠标悬停在光标下方添加图片

来自分类Dev

将鼠标悬停在标题列数据表上时显示工具提示

来自分类Dev

取消将鼠标悬停在图像上的部分模糊

来自分类Dev

将鼠标悬停在图片文字上

来自分类Dev

将鼠标悬停在触摸屏上

来自分类Dev

将鼠标悬停在非矩形div上

来自分类Dev

使用StatusBar将鼠标悬停在按钮上

来自分类Dev

将鼠标悬停在Div上以显示隐藏的内容

来自分类Dev

JavaScript对将鼠标悬停在图像上的影响

来自分类Dev

将鼠标悬停在矩形边界上

Related 相关文章

热门标签

归档