使用JS实现简单的表格的增加删除全选反选以及高亮效果
2017-09-22 11:13
1446 查看
友情提示:运行代码还需在网上下载jquery.min.js文件并把html文件反正同一文件下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
table,tr,th,td{
padding: 0;
margin: 0;
}
table {
border: 2px solid #aaa;
width: 500px;
text-align: center;
}
tr,th,td {
border: 2px solid #aaa;
}
.pink {
background-color: pink;
}
.blue {
background-color: blue;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th><input type="checkbox" name="checked" id="checked">全选</th>
<th><input type="checkbox" name="checked1" id="checked1">反选</th>
<th><button id="btn_add">添加</button></th>
</tr>
<tr>
<th></th>
<th>序号</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="check"></td>
<td>1</td>
<td><button class="cla_del">删除</button></td>
</tr>
</tbody>
</table>
</body>
<script src="jquery.min.js"></script>
<script>
$(function () {
//添加
$('#btn_add').click(function () {
var id = $('tbody>tr:last>td:eq(1)').text()*1+1;
var content = '<tr>'+
'<td>&
4000
lt;input type="checkbox" name="check"></td>'+
'<td>'+id+'</td>'+
'<td><button class="cla_del">删除</button></td>'+
'</tr>';
$('tbody').append(content);
});
//删除
$('tbody').on('click','.cla_del',function () {
$(this).parents('tr').remove();
});
//荧光棒效果
$('tbody').on('hover','tr',function(){
$(this).toggleClass('pink');
});
//全选
$('#checked').click(function () {
$('input[name=check]').prop('checked',$(this).prop('checked'));
$('input[name=check]').each(function () {
$(this).parents('tr').toggleClass('pink');
})
});
//反选
$('#checked1').click(function () {
$('input[name=check]').each(function () {
var check = $(this).prop('checked');
$(this).prop('checked',!check);
$(this).parents('tr').toggleClass('pink');
})
});
//单选
$('tbody').on('click','input[name=check]',function () {
$(this).parents('tr').toggleClass('pink');
});
/*$('table').on('click','input[type=checkbox]',function () {
$('tbody [name=check]').each(function () {
if ($(this).prop('checked')) {
$(this).parents('tr').addClass('blue');
} else {
$(this).parents('tr').removeClass('blue');
}
})
})*/
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
table,tr,th,td{
padding: 0;
margin: 0;
}
table {
border: 2px solid #aaa;
width: 500px;
text-align: center;
}
tr,th,td {
border: 2px solid #aaa;
}
.pink {
background-color: pink;
}
.blue {
background-color: blue;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th><input type="checkbox" name="checked" id="checked">全选</th>
<th><input type="checkbox" name="checked1" id="checked1">反选</th>
<th><button id="btn_add">添加</button></th>
</tr>
<tr>
<th></th>
<th>序号</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="check"></td>
<td>1</td>
<td><button class="cla_del">删除</button></td>
</tr>
</tbody>
</table>
</body>
<script src="jquery.min.js"></script>
<script>
$(function () {
//添加
$('#btn_add').click(function () {
var id = $('tbody>tr:last>td:eq(1)').text()*1+1;
var content = '<tr>'+
'<td>&
4000
lt;input type="checkbox" name="check"></td>'+
'<td>'+id+'</td>'+
'<td><button class="cla_del">删除</button></td>'+
'</tr>';
$('tbody').append(content);
});
//删除
$('tbody').on('click','.cla_del',function () {
$(this).parents('tr').remove();
});
//荧光棒效果
$('tbody').on('hover','tr',function(){
$(this).toggleClass('pink');
});
//全选
$('#checked').click(function () {
$('input[name=check]').prop('checked',$(this).prop('checked'));
$('input[name=check]').each(function () {
$(this).parents('tr').toggleClass('pink');
})
});
//反选
$('#checked1').click(function () {
$('input[name=check]').each(function () {
var check = $(this).prop('checked');
$(this).prop('checked',!check);
$(this).parents('tr').toggleClass('pink');
})
});
//单选
$('tbody').on('click','input[name=check]',function () {
$(this).parents('tr').toggleClass('pink');
});
/*$('table').on('click','input[type=checkbox]',function () {
$('tbody [name=check]').each(function () {
if ($(this).prop('checked')) {
$(this).parents('tr').addClass('blue');
} else {
$(this).parents('tr').removeClass('blue');
}
})
})*/
})
</script>
</html>
相关文章推荐
- 使用js实现表格增加与删除
- js实现表格的全选,反选,删除所选行以及隔行变色
- js 简单实现表单数据的增添,单项删改,多项删除,修改以及全选功能
- 实现隔行变色效果升级以及实现表格全选、反选、删除效果
- jQuery实现表格全选、反选、删除一行或多行、增加一行
- Jquery 实现表格全选 反选 取消全选 删除行 增加行
- JS 实现简单的Table表格分页效果(假分页)
- JQuery 增加、删除表格div层文本内容的JS代码 和仿select个性下拉框选择效果JS代码
- JS实现的表格行鼠标点击高亮效果代码
- 基于jquery的实现简单的表格中增加或删除下一行
- jquery 实现表格内的tr与td,动态添加与删除. 以及序号增加与减少,大多用在Form里
- 使用js实现的简单拖拽效果
- js实现表格隔行变色,鼠标在该行放上移走的变色效果,还有全选,反选等
- js实现表单项的全选、反选以及删除操作
- JS在GridView中实现CheckBox全选和非全选 及Gridview批量删除使用技巧
- js实现表格隔行变色,鼠标在该行放上移走的变色效果,还有全选,反选等
- js实现表单项的全选、反选以及删除操作
- js实现表格隔行变色,鼠标在该行放上移走的变色效果,还有全选,反选等
- atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html js --attilax总结
- js简单实现删除记录时的提示效果