列表生成器(js)2

在这里插入图片描述

已经傻掉的我:虽然这个功能已经基本可以实现,但是这个错误,我实在是找不出来是哪里出现的问题:(还请大佬多多指教!!!)
在这里插入图片描述
我也自己进行查找,可能是我理解的问题,还是不知道如何操作!!
在这里插入图片描述
改良的代码如下:
css代码:
.list{
display:inline-block;
background:#fff;
color:#333;
text-align:left;
font-size:20px;
}
.list-ul{
list-style:none;
margin:0;
padding:0;
}
.list-option{
padding:6px 0;
}
.list-input{
width:300px;
border:1px solid #ccc;
padding:4px;
font-size:14px;
color:#333;
}
.list-input:hover{
background:#effaff;
}
.list-btn span{
color:#0065A0;
cursor:pointer;
}
.list-btn span:hover{
text-decoration:underline;
}
.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:20px;
}
.list-add-add{
cursor:pointer;
}
.list-add-cancel{
cursor:pointer;
}
.list-add-input{
width:180px;
border:1px solid #ccc;
padding:4px;
font-size:14px;
color:#333;
}
.list-add-input:hover{background:#effaff;}
.list-hide{display:none;}

body代码:

  • [上移] [下移] [删除]
添加项目
添加到列表:
			</div>
		</div>
		
	</form>	
	
	<script src="SmartList.js"></script>
	<script type="text/javascript">
	 SmartList('list',['PHP','JavaScript']);
	</script>

SmartList.js文件:

(function (window){
var Smartlist=function(prefix,defList){//构造函数
Find.prototype.prefix=prefix;
var find=new Find(document.getElementsByClassName(prefix)[0]);//获取list对象
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 List(tmp){//创建列表对象
	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);
		//将value添加到list-input的value属性中
		find.className('input').value=value;
		var obj=this.obj;//父节点
		find.className('up').onclick=function(){
			var prev=find.prev();
			if(prev){
				obj.insertBefore(tmp,prev);
				
			}else{
				alert('已经是第一个');
				
			}
		};
		find.className('down').onclick=function(){
			var next=finf.next();
			if(next){
				obj.insertBefore(next,tmp);
				
			}else{
				alert('已经是最后一个');
				
			}
		};
		find.className('del').onclick=function(){
			if(confirm('您确定要删除?')){
				obj.removeChild(tmp);
		}
	};
	//将创建的列表项添加到列表末尾
	this.obj.appendChild(tmp);
	}
};

function Find(obj){
	this.obj=obj;
}
Find.prototype={
	prefix:'',
	className: function(className){
		return this.obj.getElementsByClassName(this.prefix+'-'+className)[0];//返回list-option对象
	},
	prev:function(){
		var node=this.obj.previousSibling;//获取当前对象的前一个节点
		while(node){
			if(node.nodeType===Node.ELEMENT_NODE){
				break;
			}
			node=node.previousSibling;
			
		}
		return node;
		
	},
	net:function(){
		var node=this.obj.nextSibling;//获取当前对象的后一各节点
		while(node){
			if(node.nodeType===Node.ELEMENT_NODE){//检测是否为节点
				break;
			}
			node=node.nextSibling;
		}
		return node;
	},
	
};
window['SmartList']=Smartlist;

})(window);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值