用DOM相关方法创建的留言板

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#ul1 {margin:0; padding:0;}
#ul1 li {list-style:none; width:300px; background:#CCC; border:1px solid #999; position:relative;}
#ul1 li h2 {display:inline-block;}
#ul1 li p {display:inline-block;}
#ul1 li a {position:absolute; right:4px; bottom:4px;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function (){
var oName=document.getElementById('name');
var oContent=document.getElementById('content');
var oUl=document.getElementById('ul1');
var oBtn=document.getElementById('btn1');
var aLi=oUl.getElementsByTagName('li');
var oLi=null; oBtn.onclick=function (){
oLi=document.createElement('li');
var oH2=document.createElement('h2');
var oP=document.createElement('p');
var oA=document.createElement('a'); oH2.innerHTML=oName.value+':';
oP.innerHTML=oContent.value;
oA.innerHTML='删除';
oA.href='javascript:;';
oA.onclick=function (){
oUl.removeChild(this.parentNode);
}; oLi.appendChild(oH2);
oLi.appendChild(oP);
oLi.appendChild(oA); if(aLi.length>0){
oUl.insertBefore(oLi, aLi[0]); //确保新添加的在最前面
}
else{
oUl.appendChild(oLi);
}
};
};
</script>
</head>
<body>
姓名:<input id="name" type="text" /><br />
内容:<textarea id="content" rows="5" cols="40"></textarea><br />
<input id="btn1" type="button" value="留言" />
<ul id="ul1">
</ul>
</body>
</html>
05-11 14:01