jQuery动态添加与删除tr行实例代码
2019/6/27 21:42:13
本文主要是介绍jQuery动态添加与删除tr行实例代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
这篇文章给大家分享的是一个用Jquery实现动态添加和删除tr行的小例子
下面是实现的样子,当然没有样式:
点击添加按钮可以添加一行:
点击删除可以删除本行:
基本功能是这样,下面是代码:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- 这里是引用了一个百度的jquery库,可换为本地jquery库 --> <script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script> </head> <body> <table id="allDatas"> <tr> <th>选择图片</th> <th>图片名称</th> <th>描述</th> <th>操作</th> </tr> <tr> <td><input type="file" name="imageFile1"/></td> <td><input name="imageName"></input></td> <td><input name="note" /> </td> <td><a href="javascript:;" onclick="deleteCurrentRow(this);"><font color='red'>删除</font></a> </td> </tr> </table> <button onclick="javascript:addCurrentRow();" >添加</button> <script> function addCurrentRow() { var trcomp="<tr><td><input type='file' name='imageFile'/></td><td><input name='imageName'></input></td><td><input name='note' /></td><td><a href='javascript:;' onclick='deleteCurrentRow(this);'><font color='red'>删除</font></a></td></tr>"; $("#allDatas tr:last-child").after(trcomp); } function deleteCurrentRow(obj) { var isDelete=confirm("真的要删除吗?"); if(isDelete) { var tr=obj.parentNode.parentNode; var tbody=tr.parentNode; tbody.removeChild(tr); } } </script> </body> </html>
总结
以上就是jQuery动态添加与删除tr行的全部内容,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。
这篇关于jQuery动态添加与删除tr行实例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set