如何通过单击同一行中某列的图像来删除行?
这是添加行的代码
function addrow() {
var table = document.getElementById("tablelist");
var row = table.insertRow(-1);
var cells = new Array();
for (var i = 0; i < 6; i++) {
cells[i] = row.insertCell(i);
cells[i].innerHTML = "New";
}
cells[6] = row.insertCell(6);
cells[6].innerHTML = '<img id="pencil" src="images/pencil-black.png"></img><img id="lock" src="images/lock-black.png"></img><img id="bin" src="images/bin-black.png"></img>';
}
这是我的jquery代码,但它不起作用。
$(document).ready(function() {
$(".userbox").on({
mouseenter : function() {
$(this).find('img').each(function() {
$(this)
.attr(
'src',
$(this).attr('src').replace('-black.png',
'-white.png'));
});
},
mouseleave : function() {
$(this).find('img').each(function() {
$(this)
.attr(
'src',
$(this).attr('src').replace('-white.png',
'-black.png'));
});
}
}, 'tr');
$("tr").on('tr', '.bin', function() {
$(this).slideUp();
});
});
注意:我知道.slideUp()
没有删除一行。
答案 0 :(得分:3)
id在文档中必须是唯一的,因此您需要将其更改为类属性
然后您需要使用委派的事件注册
$('#tablelist').on('click', '.bin', function(){
$(this).closest('tr').slideUp();
})
演示:Fiddle
答案 1 :(得分:1)
$('table').on('click', 'img', function() {
$(this).closest('tr').remove();
});
ps slideUp()
动画不适用于tr
元素