JQuery实现对html结点的操作(创建,添加,删除)
效果图:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery-3.4.1.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
#install{
display: none;
}
#save,#cancel{
width: 100px;
height: 30px;
}
.add{
margin:10px 10px;
}
.add span{
margin:10px 10px;
}
</style>
</head>
<body>
<input type="button" id="add_btn" value="添加" />
<table border="" cellspacing="" cellpadding="">
<tr>
<th>编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
<th>操作</th>
</tr>
<tr>
<td>1</td>
<td>华为</td>
<td>¥2000</td>
<td>描述1</td>
<td>
<a href="#">编辑</a>|<a href="#">删除</a>
</td>
</tr>
<tr>
<td>2</td>
<td>华为</td>
<td>¥2000</td>
<td>描述2</td>
<td>
<a href="#">编辑</a>|<a href="#">删除</a>
</td>
</tr>
<tr>
<td>3</td>
<td>华为</td>
<td>¥2000</td>
<td>描述3</td>
<td>
<a href="#">编辑</a>|<a href="#">删除</a>
</td>
</tr>
</table>
<div id="install">
<div class="add">
<h1>操作</h1>
</div>
<div class="add"><span>名称</span><input type="text" class="name" /></div>
<div class="add"><span>价格</span><input type="text" class="price" /></div>
<div class="add"><span>描述</span><input type="text" class="describe" /></div>
<div class="add">
<input type="button" id="save" value="保存" />
<input type="button" id="cancel" value="取消" />
</div>
</div>
<script type="text/javascript">
//修改完列表内容需要给号码排序
function loop () {
var num = $('tr td').length;
console.log(num)
for (var i=0;i<num;i+=5) {
$('tr td').eq(i).text(i/5+1);
}
}
//添加事件
$('#add_btn').click(function () {
$('#install').show();
type='tianjia';
});
//记录是第几个修改的结点
var select;
//记录当前的结点
var node;
//记录是修改是添加
var type;
//删除和修改
$('tr td a').click(function() {
// console.log(this.text())
//找节点删除
if($(this).text() == '删除') {
$(this).parent().parent().remove();
//记得循环前面的列表号
loop();
} else {
//给修改的时候赋值
select=$(this).parent().prev().prev().prev().prev().text()
node=$(this);
$('#install').show();
console.log(select)
$('.name').val($(this).parent().prev().prev().prev().text())
$('.price').val($(this).parent().prev().prev().text())
$('.describe').val($(this).parent().prev().text())
type = 'xiugai';
}
// this.parent().remove();
})
$("#save").click(function () {
if(type=='xiugai'){
//修改的时候赋值
node.parent().prev().prev().text($('.name').val())
node.parent(). prev().prev().text($('.price').val())
node.parent() .prev().text($('.describe').val())
alert('保存成功')
//修改完清空
$('.name').val('')
$('.price').val('')
$('.describe').val('')
$('#install').hide();
}else{
//创建所需结点
var tr_ = $('<tr></tr>');
var td_num = $('<td></td>');
var td_name = $('<td></td>');
var td_price = $('<td></td>');
var td_describe = $('<td></td>');
var td_a = $('<td><a href="#">编辑</a>|<a href="#">删除</a></td>');
var num = $('tr td').length;
//给节点赋值
td_num.text(num);
td_name.text($('.name').val())
td_price.text($('.price').val());
td_describe.text($('.describe').val());
//结点进行结构构造
tr_.append(td_num);
tr_.append(td_name);
tr_.append(td_price);
tr_.append(td_describe);
tr_.append(td_a);
//给table表插入数据
$('table').last().append(tr_);
}
loop();
})
//点取消按钮清空添加表
$("#cancel").click(function () {
$('.name').val('')
$('.price').val('')
$('.describe').val('')
$('#install').hide();
})
</script>
</body>
</html>
JQuery实现对html结点的操作(创建,添加,删除)的更多相关文章
- 通过vb.net 和NPOI实现对excel的读操作
通过vb.net 和NPOI实现对excel的读操作,很久很久前用过vb,这次朋友的代码是vb.net写的需要一个excel的操作, 就顾着着实现功能了,大家凑合着看吧 Option Explicit ...
- Python中实现对list做减法操作介绍
Python中实现对list做减法操作介绍 这篇文章主要介绍了Python中实现对list做减法操作介绍,需要的朋友可以参考下 问题描述:假设我有这样两个list, 一个是list1,list1 = ...
- dir(dict)|字典的创建-添加-删除-修改-判断存在-取值等相关操作
dir(dict) ####字典操作:创建-添加-删除-修改-判断存在-取值 #(一)创建字典: {} .等号. zip(). [(),()] #1.创建空字典 dict0 = {} #2.等号创建 ...
- 使用代理实现对C# list distinct操作
范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复.而一般的如果要对list去重复如果使用linq dist ...
- Jquery实现对select的操作
select实现对文本框的显示和隐藏 /** * 通过select的值实现对文本框的显示和隐藏 * #id为一个select控件 * .obj为一个文本框 */ function initSelect ...
- 利用C#实现对excel的写操作
一.COM interop 首先我们要了解下何为COM Interop,它是一种服务,可以使.NET Framework对象能够与COM对象通信.Visual Studio .NET 通过引入面向公共 ...
- PHP实现对MongoDB的基础操作
PHP扩展 PHP5.2.PH ...
- 使用POI来实现对Excel的读写操作
事实上我感觉直接贴代码就好了.代码里面差点儿做到每一行一个凝视.应该看起来会比較简单 代码托管在github上:https://github.com/chsj1/ExcelUtils package ...
- XMLHelper类 源码(XML文档帮助类,静态方法,实现对XML文档的创建,及节点和属性的增、删、改、查)
以下是代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
随机推荐
- 如何将Altera官方提供的CADENCE.OLB应用于altium Designer中
- [hdu5371 Hotaru's problem]最大回文半径
题意:在一个字符串里面找最长的[A][B][A]子串,其中[A][B]是回文串,[A]和[B]的长度相等 思路:[A][B]是回文串,所以[B][A]也是回文串.先预处理出每个点的最大回文半径Ri,枚 ...
- vue项目中使用bpmn-流程图xml文件中节点属性转json结构
内容概述 本系列“vue项目中使用bpmn-xxxx”分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具 ...
- 最短路径——floyd算法代码(c语言)
最短路径问题 昨天自己试了试写一下dijkstra的算法博客 dijkstra链接在这← 今天来更floyd算法,感觉非常简单果然暴力才是解决一切的王道 一.总体思想 floyd算法就是每一次从邻接矩 ...
- Codeforces1176A(A题)Divide it!
Divide it! You are given an integer nn. You can perform any of the following operations with this nu ...
- centos7 git下载速度慢
nslookup命令 yum -y install bind-utils [root@iZ1i4qd6oynml0Z ~]# nslookup github.global.ssl.fastly.Net ...
- day02:三元运算、布林非、列表等(20170214)
#1:三元运算(满足条件就返回值,不简洁的代码):a= 1b= 3c= 5if a > b : d = aelse: d = cprint (d) #2:三元运算(满足条件就返回值,简洁的代码) ...
- Linux下分析bin文件的10种方法
这世界有10种人,一种人懂二进制,另一种人不懂二进制. --鲁迅 大家好,我是良许. 二进制文件是我们几乎每天都需要打交道的文件类型,但很少人知道他们的工作原理.这里所讲的二进制文件,是指一些可执行文 ...
- UIStackView上手教程
https://www.jianshu.com/p/19fbf3ee2840 https://www.cnblogs.com/bokeyuanlibin/p/5693575.html https:// ...
- PAT-1058 A+B in Hogwarts (进制转换)
1058. A+B in Hogwarts If you are a fan of Harry Potter, you would know the world of magic has its ow ...