代码如下:
<div id="one" style="background:#CCC;width:500px;height:300px;"></div>
<input type="button" value="add" id="btn1">
<input type="button" value="delete" id="btn2">
<script>
var add=document.getElementById("btn1");
var del=document.getElementById("btn2");
add.onclick=function(){
var po=document.getElementById("one");
var con=document.createElement("div");
con.innerHTML="hellow world!!";
po.appendChild(con);
}
del.onclick=function(){
var po=document.getElementById("one");
var children=po.childNodes;
for(i=0;i<children.length;i++){
po.removeChild(children[i]);
}
}</script>每次按下add按钮,就增加一个hellow world。每次按下delete就删除所有的hellow world。但是,为什么我每次按下delete不是删除所有的hellow world,而是一部分?
<div id="one" style="background:#CCC;width:500px;height:300px;"></div>
<input type="button" value="add" id="btn1">
<input type="button" value="delete" id="btn2">
<script>
var add=document.getElementById("btn1");
var del=document.getElementById("btn2");
add.onclick=function(){
var po=document.getElementById("one");
var con=document.createElement("div");
con.innerHTML="hellow world!!";
po.appendChild(con);
}
del.onclick=function(){
var po=document.getElementById("one");
var children=po.childNodes;
for(i=0;i<children.length;i++){
po.removeChild(children[i]);
}
}</script>每次按下add按钮,就增加一个hellow world。每次按下delete就删除所有的hellow world。但是,为什么我每次按下delete不是删除所有的hellow world,而是一部分?
解决方案 »
- 请教一个在点击回复,插入回复框的问题
- 技术性难题,怎么撤销
- 类似这种搜索原理是什么?
- 如何把這討論區的頭像圖示,換成外部連結的圖示
- HTML标签中如何获得javascript变量
- 关于弹出小窗口的问题(就这点分了 请高手谅解)
- 如何通过html页面中的按钮来控制网页中的flash,使其读取不同xml文件中的所存储的图片路径
- 对于我来说很难,但对于大家很简单的一个问题--------------------谢谢!!
- 各位快来帮我一下。
- netscape中怎么通过URL来打开一个服务器上的eml文件并自动对它解码并进行显示
- 这段不符合W3C标准的漂浮广告代码修改成支持W3C标准?
- 下面的HTML为什么报错呢?
var po=document.getElementById("one");
var children=po.childNodes; for(i=children.length-1;i>=0;i--){
alert(i)
po.removeChild(children[i]);
}
}应该从最后一个元素删除,如果从第一个删除的话 ,会改变原有dom的结构,索引都会改变的
var po=document.getElementById("one");
var children=po.childNodes;
while(children.length) po.removeChild(children[0]);
}
//按你意思,可以简化成
del.onclick=function(){
var po=document.getElementById("one");
po.innerHTML=""
}
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/JavaScript">
$(function(){
$("#btn1").click(function(){
$("#one").append("hellow world");
});
$("#btn2").click(function(){
$("#one").empty();
});
})
</script> <div id="one" style="background:#CCC;width:500px;height:300px;"></div>
<input type="button" value="add" id="btn1">
<input type="button" value="delete" id="btn2">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head><body>
<div id="one" style="background:#CCC;width:500px;height:300px;"></div>
<input type="button" value="add" id="btn1">
<input type="button" value="delete" id="btn2">
<script type="text/javascript">
var add=document.getElementById("btn1");
var del=document.getElementById("btn2");
add.onclick=function(){
var po=document.getElementById("one");
var con=document.createElement("div");
con.innerHTML="hellow world!!";
po.appendChild(con);
}
del.onclick=function(){
var po=document.getElementById("one");
var children=po.childNodes;
var p=children.length;
for(i=0;i<p;i++){
po.removeChild(children[0]);
}
}
</script>
</body>
</html>
<head>
</head>
<body>
<div id="one" style="background:#CCC;width:500px;height:300px;"></div>
<input type="button" value="add" id="btn1">
<input type="button" value="delete" id="btn2">
<script>
var add=document.getElementById("btn1");
var del=document.getElementById("btn2");
add.onclick=function(){
var po=document.getElementById("one");
var con=document.createElement("div");
con.innerHTML="hellow world!!";
po.appendChild(con);
}
del.onclick=function(){
var po=document.getElementById("one");
var children=po.childNodes;
while(po.childNodes && po.childNodes.length>0)
{
po.removeChild(po.childNodes[0]);
}
}
</script>
</body>
</html>
1.for(i=0;i<children.length;i++){
children[i].innerHTML="";
}2.for(i=children.length-1;i>=0;i--){
po.removeChild(children[i]);
}
3.while(po.childNodes && po.childNodes.length>0)
{
po.removeChild(po.childNodes[0]);
}谢谢,各位的帮助