JavaScript实现项目列表的增删移动

JavaScript实现项目列表的增删移动

在前面我们已经学习了html和css,相信大家对此应该有了相当的理解
想要完成此项任务我们需要准备以下知识点:

Find()定义和用法

find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

find() 方法为数组中的每个元素都调用一次函数执行:
当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
如果没有符合条件的元素返回 undefined
当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
如果没有符合条件的元素返回 undefined
find()语法:
array.find(function(currentValue, index, arr),thisValue)
currentValue表示当前元素,index表示当前元素的索引值,arr表示当前元素所属的数组对象,thisValue默认值是this表示当前对象

insertBefore() 方法

insertBefore() 方法在你指定的已有子节点之前插入新的子节点
insertBefore()语法
node.insertBefore(newnode,existingnode)
newnode表示需要插入的节点对象。
existingnode 在其之前插入新节点的子节点。如果未规定,则 insertBefore 方法会在结尾插入 newnode。
Node 对象表示你插入的节点。

思路分析
想要完成这个项目首先我们要明白的是这属于html中的form标签知识,因此我们可以在外层套一个Div标签,然后在它里面插入一个ul标签里面下面用一个li标签来装input标签输入的内容,旁边的那些功能键我们就用span标签然后用js定义function来实现各种功能。这样我们就完成了上半部分。
下半部分我们同样是利用Div和span标签来完成布局。

运行结果:
在这里插入图片描述
运行代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>js项目列表的增删和移动</title>
		<style>
			body{background:#ddd;text-align:center}
			.list{display:inline-block;margin-top:20px;padding:40px;border-radius:8px;background:lightsalmon;color:#000000;text-align:left;font-size:13px}
			.list-ul{list-style:none;margin:0;padding:0}
			.list-option{padding:6px 0;}
			.list-input{width:300px;border:1px solid #00FFFF;padding:5px;font-size:16px;color:#333}
			.list-input:hover{background:#effaff}
			.list-btn span{color:#0065A0;;cursor:pointer}
			.list-btn span:hover{text-decoration:underline}
			.list-btn b{text-align:center;background-color:#D6EDFF;border-radius:6px;width:20px;height:20px;display:inline-block;margin:0 2px;cursor:pointer;color:#238FCE;border:1px solid #B3DBF8;float:left}
			.list-bottom{margin-top:5px}
			.list-add-show{color:#f60;cursor:pointer}
			.list-add-show:before{position:relative;top:1px;margin-right:5px;content:"+";font-weight:700;font-size:16px;font-family:arial}
			.list-add-show span:hover{text-decoration:underline}
			.list-add-area{margin-top:5px}
			.list-add-add{cursor:pointer;margin-left:5px}
			.list-add-cancel{cursor:pointer;margin-left:4px}
			.list-add-input{width:180px;border:1px solid #ccc;padding:4px;font-size:14px;color:#333}
			.list-add-input:hover{background:#effaff}
			.list-tmp{display:none}
			.list-hide{display:none}
    </style>
	</head>
	<body>
		<form action="" method="">  
			<div class="list">
				<ul class="list-ul">
					<li class="list-option">
						<input type="text" class="list-input" name="list[]" id="" value="" />
						<span class="list-btn">
							<span class="list-up"> [上移]</span>
							<span class="list-down"> [下移]</span>
							<span class="list-del"> [删除]</span>
						</span>
					</li>
				</ul>
				<div class="list-bottom" id="">
					<span class="list-add-show">添加项目</span>
					<div class="list-add-area list-hide">
						添加到列表
						<input type="text" class="list-add-input" name="list[]" id="list[]" value="" />
						<input type="button" class="list-add-add"  value="添加" />
						<input class="list-add-cancel" type="button" value="取消" />
						
					</div>
				</div>
			</div>
		</form><script type="text/javascript">
			(function(window) {
				var SmartList = function(prefix, defList) {
					Find.prototype.prefix = prefix;
					var find = new Find(document.getElementsByClassName(prefix)[0]); // 获取对象
					var list = new List(find.className('option'));
					for (var i in defList) {
						list.add(defList[i]);
					}
					
					var add = {
						'show':find.className('add-show'),	
						'area':find.className('add-area'),	
						'input':find.className('add-input'),	
						'add':find.className('add-add'),	
						'cancel':find.className('add-cancel')	
					};
					add.show.onclick = function() {	
						add.area.classList.remove(prefix + '-hide');
					};
					add.add.onclick = function()  {	
						list.add(add.input.value);
						
					};
					add.cancel.onclick =  function() {		
						add.area.classList.add(prefix + '-hide');
					};
				};
				function Find(obj) {
					this.obj = obj;
				}
				Find.prototype = {
					prefix: '', //待查找的前缀
					className: function(className) {
						return this.obj.getElementsByClassName(this.prefix + '-' + className)[0];
					},
					prev: function() {
						var node = this.obj.previousSibling; //	获取当前元素对象的前一个节点
						while (node) {
							if (node.nodeType === Node.ELEMENT_NODE) {
								break;
							}
							node = node.previousSibling;
						}
						return node;
					},
			 
					next: function() {
						var node = this.obj.nextElementSibling; //获取当前元素对象的后一个节点
						while (node) {
							if (node.nodeType === Node.ELEMENT_NODE) {
								break;
							}
							node = node.nextElementSibling;
						}
						return node;
					}
				}
				function List(tmp) { // List构造函数 参数tmp表示操作的<li>元素模板
					this.tmp = tmp;
					this.obj = tmp.parentNode; // 保存模板的父节点对象
					this.obj.removeChild(tmp); //从<ul>中移除<li>模板
				}
				List.prototype = {
					add: function(value) {
						var tmp = this.tmp.cloneNode(true); //克隆一个元素节点
						var find = new Find(tmp);
						find.className('input').value = value;
						var obj = this.obj; //获取ul元素对象
						find.className('up').onclick = function() { //添加上移单击事件
							var prev = find.prev(); //获取前一个节点
							if (prev) {
								obj.insertBefore(tmp, prev); //在prev前插入tmp
							} else {
								alert('这已经是第一张了');
							}
						};
						find.className('down').onclick = function() { 
							var last = find.next(); 
							if (last) {
								obj.insertBefore(next, tmp); 
							} else {
								alert('这已经是最后一张了');
							}
						};
						find.className('del').onclick = function() { 
							if (confirm('你确定要删除吗?')) {
								obj.removeChild(tmp);
							}
						}
						this.obj.appendChild(tmp);
					}
			 
				};
				window['SmartList'] = SmartList;
			})(window);
			SmartList('list', ['Java', 'Linux']);  //添加默认项目
		</script>
			
	</body>
</html>
 
  • 13
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值