悬停在动态添加的表行上的效果
Hover effect on dynamically added table row
我有一个表,它在表单元格上有一个悬停事件,用于切换标题单元格中的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')
而不是cells
(cells.on
->$('body').on
)。
使用以下代码:Jsfidle
$('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');
相关文章:
- 如何在Google柱状图中动态添加行/列
- 使用jQuery动态添加表并在其中动态添加行
- 如何使用Angular动态添加iframe-src
- 如何向JSON数组动态添加属性
- 动态添加通过json创建的选择项
- 使用jQuery根据动态生成的html动态添加html
- 使用.on动态添加jquery/js不知道的html元素
- JS动态添加字段-删除按钮不起作用
- 动态添加AngularJS脚本
- 将表行动态添加到表或tbody元素中
- jQuery-通过AJAX调用动态添加具有值的表单元格
- 将特定属性动态添加到元素中
- 在highcharts.js中向点弹出窗口动态添加文本
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- 使用jQuery动态添加onClick事件
- 如何在Opencart中动态添加JavaScript
- 剑道UI下载列表:如何动态添加新元素
- 动态添加复选框和输入框 jQuery
- 动态添加哈希标记;t工作
- 将图表动态添加到组合高图表中