jQuery实现表格的增、删、改操作示例
网络编程 2021-07-04 15:50www.168986.cn编程入门
这篇文章主要介绍了jQuery实现表格的增、删、改操作,涉及基于jQuery的事件响应及页面元素动态操作相关实现技巧,需要的朋友可以参考下
本文实例讲述了jQuery实现表格的增、删、改操作。分享给大家供大家参考,具体如下:
这里实现的是在jQuery中通过按钮的形式,对表格进行的一些基本操作,可以实现表格的增删改操作,并实现对鼠标事件监听,实现表格的高亮行操作。
<head> <meta charset="UTF-8"> <title>.jb51. jQuery表格操作</title> <script src="http://libs.baidu./jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { //添加一行 $("#one").click(function() { var $td = $("#trOne").clone(); $("table").append($td); $("table tr:last").find("input").val(""); }); //删除一行 $("#two").click(function() { $("table tr:not(:first):last").remove(); }); //删除所有行 $("#three").click(function() { /*var len=$("tr").length; for(var i=0;i<=len;i++){ $("tr")[i].remove(); }*/ //除第一行为其它行删除 $("tr:not(:first)").remove(); }); //删除选中的行 $("#four").click(function() { //遍历选中的checkbox $("[type='checkbox']:checked").each(function() { //获取checkbox所在行的顺序 n = $(this).parents("tr").index(); $("table").find("tr:eq(" + n + ")").remove(); }); }); //设置高亮行 $("tr").mouseover(function() { $(this).css("background-color","red"); }); $("tr").mouseout(function(){ $(this).css("background-color","white"); }); }); </script> </head> <body> <input type="button" id="one" value="添加一行" /><br /> <input type="button" id="two" value="删除一行" /><br /> <input type="button" id="three" value="删除所有行" /><br /> <input type="button" id="four" value="删除选中的行" /><br /> <table width="400px" height="50px" border="2px" cellspacing="0" cellpadding="0"> <tr id="trOne"> <td><input type="checkbox" name=""></td> <td><input type="" name="" value="姓名" </td> <td><input type="" name="" value="年龄" </td> <td><input type="" name="" value="性别" </td> </tr> <tr> <td><input type="checkbox" name=""></td> <td><input type="" name="" value="张三" </td> <td><input type="" name="" value="18" </td> <td><input type="" name="" value="男" </td> </tr> <tr> <td><input type="checkbox" name=""></td> <td><input type="" name="" value="李四" </td> <td><input type="" name="" value="18" </td> <td><input type="" name="" value="男" </td> </tr> <tr> <td><input type="checkbox" name=""></td> <td><input type="" name="" value="王五" </td> <td><input type="" name="" value="18" </td> <td><input type="" name="" value="男" </td> </tr> </table> </body>
效果图如下:
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:测试上述代码运行效果。
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》
希望本文所述对大家jQuery程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程