<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
position: relative;
}
#top{
margin-left: 60px;
margin-top: 60px;
}
#top div{
width: 200px;
height: 50px;
color: white;
font-size: 20px;
margin-bottom: 2px;
text-align: center;
line-height: 50px;
}
#top div font{
position: absolute;
right: 3px;
/*將光標(biāo)變成手*/
cursor: pointer;
}
#bottom{
margin-left: 60px;
}
#bottom #text{
display: inline-block;
width: 200px;
height: 50px;
border: none;
outline: none;
/*讓光標(biāo)在中間*/
text-align: center;
border-bottom: 2px solid lightgrey;
font-size: 16px;
}
#bottom #button{
display: inline-block;
width: 100px;
height: 30px;
border: none;
outline: none;
background-color: coral;
color: white;
}
</style>
</head>
<body>
<div id="top">
</div>
<!--添加默認(rèn)的水果標(biāo)簽-->
<script type="text/javascript">
var fruitArray = ['香蕉', '蘋果', '草莓', '火龍果'];
for(index in fruitArray){
fruitName = fruitArray[index];
creatFruitNode(fruitName, 'darkgreen')
}
//==========刪除水果=============
function delFruit(){
//在這兒,點(diǎn)擊的是哪個(gè)標(biāo)簽this就指向誰
this.parentElement.remove()
}
//添加節(jié)點(diǎn)
function creatFruitNode(fruitName, color1){
//創(chuàng)建標(biāo)簽
var fruitNode = document.createElement('div')
fruitNode.innerHTML = fruitName;
var fontNode = document.createElement('font');
fontNode.innerText = '×';
//給點(diǎn)擊事件綁定驅(qū)動(dòng)程序
fontNode.onclick = delFruit;
fruitNode.appendChild(fontNode);
//設(shè)置背景顏色
fruitNode.style.backgroundColor = color1
//添加標(biāo)簽
var topNode = document.getElementById('top')
topNode.appendChild(fruitNode)
}
</script>
<div id="bottom">
<input type="text" name="" id="text" value="" />
<input type="button" name="" id="button" value="確定" onclick="addFruit()"/>
</div>
<script type="text/javascript">
//=========產(chǎn)生隨機(jī)顏色=============
function randomColor(){
var num1 = parseInt(Math.random()*255);
var num2 = parseInt(Math.random()*255);
var num3 = parseInt(Math.random()*255);
return 'rgb('+num1+','+num2+','+num3+')';
}
//==========添加水果==============
function addFruit(){
//獲取輸入框中的內(nèi)容
var inputNode = document.getElementById('text');
var addName = inputNode.value;
if(addName.length == 0){
alert('輸入的內(nèi)容為空丽已!');
return;
}
//清空輸入框中的內(nèi)容
inputNode.value = '';
//創(chuàng)建標(biāo)簽
var fruitNode = document.createElement('div');
fruitNode.innerHTML = addName;
var fontNode = document.createElement('font');
fontNode.innerText = '×';
//給點(diǎn)擊事件綁定驅(qū)動(dòng)程序
fontNode.onclick = delFruit;
fruitNode.appendChild(fontNode);
//創(chuàng)建隨機(jī)顏色
//'rgb(255, 0, 0)'
fruitNode.style.backgroundColor = randomColor();
var topNode = document.getElementById('top');
topNode.insertBefore(fruitNode, topNode.firstChild);
}
</script>
</body>
</html>
動(dòng)態(tài)添加和刪除
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妆偏,“玉大人刃鳄,你說我怎么就攤上這事∏睿” “怎么了叔锐?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)见秽。 經(jīng)常有香客問我愉烙,道長(zhǎng),這世上最難降的妖魔是什么解取? 我笑而不...
- 正文 為了忘掉前任步责,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘勺择。我一直安慰自己创南,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布省核。 她就那樣靜靜地躺著稿辙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪气忠。 梳的紋絲不亂的頭發(fā)上邻储,一...
- 文/蒼蘭香墨 我猛地睜開眼米母,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼勾扭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起铁瞒,我...
- 序言:老撾萬榮一對(duì)情侶失蹤妙色,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后慧耍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體身辨,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年芍碧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了煌珊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站陨倡,受9級(jí)特大地震影響敛滋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜兴革,卻給世界環(huán)境...
- 文/蒙蒙 一绎晃、第九天 我趴在偏房一處隱蔽的房頂上張望蜜唾。 院中可真熱鬧,春花似錦庶艾、人聲如沸袁余。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽颖榜。三九已至,卻和暖如春煤裙,著一層夾襖步出監(jiān)牢的瞬間掩完,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓题翰,卻偏偏與公主長(zhǎng)得像恶阴,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子豹障,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 前言 最近項(xiàng)目app要在手機(jī)上實(shí)現(xiàn)編輯游記的功能存淫,同時(shí)實(shí)現(xiàn)文字,目錄沼填,圖片,視頻的編輯括授,添加和刪除坞笙,一開始想...
- 使用的時(shí)候總是容易查一下薛夜、試一下,今天想要自己總結(jié)一下版述,要是不確定直接來看看梯澜。剛說完要自己寫,結(jié)果就看到了更好的總...
- AngularJS 動(dòng)態(tài)添加元素和刪除元素 angular.element方法
- 網(wǎng)頁的基本組成結(jié)構(gòu) 網(wǎng)頁 = HTML + CSS + JavaScriptHTML:網(wǎng)頁元素內(nèi)容CSS: 控制網(wǎng)...
- 4月12日渴析,我的一天之廣播面試晚伙。 今天下午月5:05分,我們六年級(jí)20班的各位面試員就已經(jīng)做好了充分(啊呸<蠹搿)的準(zhǔn)...