在javascript中,我动态创建一个个标签,但是不知道怎么将它们一个个删除掉

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
window.onload=function(){
document.getElementById("b1").onclick=function(){
//创建一个p标签
var p1=document.createElement("p");

var text=document.createTextNode("你好,我是你的新标签");
p1.appendChild(text);
document.getElementById("div").appendChild(p1);

}//删除标签
document.getElementById("b2").onclick=function(){
var d=document.getElementById("div");
var p1=document.getElement(p1);
d.removeChild(p1);
}

}
</script>
</head>
<body>
<h2>动态创建新标签</h2>
<div id="div"></div>
<input type="button" id="b1" value="创建"/>
<input type="button" id="b2" value="删除"/>
</body>
</html>

亲,改一成这样就可以了

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
window.onload=function(){
document.getElementById("b1").onclick=function(){
//创建一个p标签
var p1=document.createElement("p");

var text=document.createTextNode("你好,我是你的新标签");
p1.appendChild(text);
document.getElementById("div").appendChild(p1);

}//删除标签
document.getElementById("b2").onclick=function(){
var d=document.getElementById("div");
//选择所有的p
var p1=d.getElementsByTagName('p');
//如果有p,则从头到尾依次删除(点击一下删除最顶端的那个,知道删完)
if(p1.length>0)
{
d.removeChild(p1[0]);
}


}

}
</script>
</head>
<body>
<h2>动态创建新标签</h2>
<div id="div"></div>
<input type="button" id="b1" value="创建"/>
<input type="button" id="b2" value="删除"/>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-03
var p1=document.getElement(p1); 这句取不到元素的;改成 var p1=document.getElementsByTagName('p')[0];追问

谢谢你,因为只能采纳一个答案,所以我只采纳第一位啦!不好意思!不过还是谢谢你的帮忙!

相似回答