添加删除

来源:互联网 发布:红色摇滚知乎 编辑:程序博客网 时间:2024/05/29 18:14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload=function(){
var oUl=document.getElementById('ul1');
var oTxt1=document.getElementById('txt1');
var oBtn=document.getElementById('btn1');
oBtn.onclick=function(){
var oLi=document.createElement('li');
oLi.innerHTML=oTxt1.value+'<a href="javascript:;">删除</a>';
oLi.children[0].onclick=function(){
oUl.removeChild(oLi);

}
if(oUl.children.length==0){
oUl.appendChild(oLi);
}else{
oUl.insertBefore(oLi,oUl.children[0]);

}
oTxt1.value='';
}
}
</script>
</head>
<body>
<input type="text" id="txt1">
<input type="button" value="添加" id="btn1">

<ul id="ul1">
</ul>
</body>
</html>
0 0