JS實(shí)現(xiàn)留言板功能
每天一個(gè)JS 小demo之留言板。主要知識(shí)點(diǎn):DOM方法的理解和運(yùn)用

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.wrap {
width: 400px;
margin: 30px auto;
}
textarea {
display: block;
width: 100%;
height: 60px;
}
input {
display: block;
width: 60%;
margin: 15px auto;
}
li {
padding: 5px 10px;
position: relative;
word-break: break-all;
}
.red {
color: #000;
background: #f1f1f1;
}
.pink {
color: #000;
background: #ccc;
}
a {
position: absolute;
right: 0;
top: -20px;
background: yellow;
color: #fff;
}
#list {
margin: 0;
padding: 0;
list-style: none;
font: 14px/26px "宋體";
}
.clos {
position: absolute;
top: 0;
right: -50px;
width: 50px;
color: #fff;
background: #000;
padding: 5px 0;
text-decoration: none;
text-align: center;
}
.clos:hover {
box-shadow: 0 0 5px rgba(0,0,0,.5)
}
</style>
<script type="text/javascript">
window.onload = function(){
var btn = document.querySelector('input');
var text = document.querySelector('textarea');
var list = document.querySelector('#list');
var colors = ["red","pink"];
var nub = 0;
btn.onclick = function(){
if(text.value.trim() == ""){
alert("打點(diǎn)字吧");
return false;
}
var li = document.createElement("li");
li.innerHTML = text.value;
// li.className = colors[nub%colors.length];
/* 判斷a標(biāo)簽已經(jīng)被添加,就讓a標(biāo)簽顯示出來(lái),否則就添加 */
if(list.children[0]&&list.children[0].className=="red"){
li.className = "pink";
} else {
li.className = "red";
}
var a = null;
li.onmouseover = function(){
if(a) {
a.style.display = "block";
} else {
a = document.createElement("a");
a.href = "javascript:;";
a.className = "clos";
a.innerHTML = "刪除";
a.onclick = function (){
list.removeChild(this.parentNode);
};
this.appendChild(a);
}
};
li.onmouseout = function(){
a.style.display = "none";
};
list.insertBefore(li,list.children[0]);
text.value = "";
nub++;
};
};
</script>
</head>
<body>
<div>
<div class="wrap">
<textarea id="text"></textarea>
<input type="button" value="創(chuàng)建元素">
<ul id="list"></ul>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js判斷ie版本號(hào)的簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要是對(duì)js判斷ie版本號(hào)的簡(jiǎn)單實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03
JavaScript實(shí)現(xiàn)二叉樹(shù)定義、遍歷及查找的方法詳解
這篇文章主要介紹了JavaScript實(shí)現(xiàn)二叉樹(shù)定義、遍歷及查找的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了二叉樹(shù)的相關(guān)概念及javascript構(gòu)建二叉樹(shù)、遍歷、查找二叉樹(shù)的常用操作技巧,需要的朋友可以參考下2017-12-12
JavaScript正則表達(dá)式替換字符串中圖片地址(img src)的方法
這篇文章主要介紹了JavaScript正則表達(dá)式替換字符串中圖片地址(img src)的方法,結(jié)合實(shí)例形式分析了JS正則替換的常用技巧與注意事項(xiàng),需要的朋友可以參考下2017-01-01
js Canvas實(shí)現(xiàn)圓形時(shí)鐘教程
這篇文章主要為大家詳細(xì)介紹了HTML5 Canvas實(shí)現(xiàn)圓形時(shí)鐘簡(jiǎn)易教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
javascript函數(shù)中的3個(gè)高級(jí)技巧
這篇文章主要為大家詳細(xì)介紹了javascript函數(shù)中的3個(gè)高級(jí)技巧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
javascript?變量聲明?var,let,const?的區(qū)別
這篇文章主要介紹了javascript?變量聲明?var,let,const?的區(qū)別,變量聲明,每種編程語(yǔ)言必不可少的語(yǔ)法,在javascript中,變量的聲明相對(duì)其他語(yǔ)言來(lái)說(shuō),算是比較簡(jiǎn)單的。更多相關(guān)的具體內(nèi)容需要的小伙伴可以參考一下2022-06-06
在原生不支持的舊環(huán)境中添加兼容的Object.keys實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇在原生不支持的舊環(huán)境中添加兼容的Object.keys實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
對(duì)比分析Django的Q查詢及AngularJS的Datatables分頁(yè)插件
通過(guò)本文給大家對(duì)比分析了Django的Q查詢及AngularJS的Datatables分頁(yè)插件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02

