<DOCTYPE html> <thml lang = "en"> <head> <meta charset = "UTF-8"> <title>空間</title> <style type = "text/css"> *{ padding : 0; margin : 0; } ul{ list-s ...
<DOCTYPE html>
<thml lang = "en">
<head>
<meta charset = "UTF-8">
<title>空間</title>
<style type = "text/css">
*{
padding : 0;
margin : 0;
}
ul{
list-style:none;
}
.box{
width : 800px;
height : auto;
border : 1px solid #ccc;
margin :100px auto;
text-align : center;
padding : 30px 0;
}
.box textarea{
width : 450px;
resize : none;//設置文本不能拖動
}
.box li{
width : 450px;
line-height : 30px;
birder-bottom : 1px dashed #ccc;
margin-left : 80px;
text-align :left;
}
.box li a {
float : right;
}
</style>
<script type = "text/javascript">
window.onload = function (){
var btn = document.getElementsByTagName("button")[0];
var txt = document.getElementsByTagName(textarea")[0];
var ul = docyment.createElement("ul");
btn.onclick = function (){
//需要判斷文本框中是否有內容
if(txt.value == ""){
alert("內容不能為空!!");
return false;//讓操作在這個地方終止
}
var newli = document.createElement("li");
newli.linerHTML = txt.value + "<a href = 'javascript:;'>
刪除</a>";
ul.appendChild(newli);
//清空輸入框
txt.value = "";
var aa = document.getElementsByTagName("a");
//獲取所有的a標簽
for(var i = 0;i< aa.length; i ++){
aa[i].onclick = function(){
this.parentNode.remove();
}
}
}
}
</script>
<body>
<div class = "box">
說說:<textarea name = "" id = "" cols = "30" rows = "10"></textarea>
<button>發表</button>
</div>
</body>
</html>