[code]<div id="test">
<div id="0">aaa</div>
<div id="1">bbb</div>
<div id="2">ccc</div>
<div id="3">ddd</div>
</div>
<script type="text/javascript">
function ChildChangeListener(){
this.lastlength=document.getElementById("test").childNodes.length;
this.start();
}
//间隔半秒监听移除事件
ChildChangeListener.prototype.start=function(){
setInterval(this.listener,500);
}
ChildChangeListener.prototype.listener=function(){
var ccl=this;
var currlength=document.getElementById("test").childNodes.length;
if(ccl.lastlength-currlength!=0){
alert(this.lastlength+","+currlength);
this.lastlength=currlength;
}
}
function startit(){
remove();
}
//间隔1秒移出一个节点
function remove(){
var test = document.getElementById("test");
if(test.childNodes.length>0){
test.removeChild(test.firstChild);
setTimeout("remove()",1000);
}
}
function add(){
var div=document.createElement("div");
div.innerHTML="new div";
var test = document.getElementById("test");
test.appendChild(div);
}
</script>
<script type="text/javascript">
var li=new ChildChangeListener();
</script>
<input type="button" onclick="startit();" value="delete"></button>
<input type="button" onclick="add();" value="add"></button>[/code][code]<div id="test">
<div id="0">aaa</div>
<div id="1">bbb</div>
<div id="2">ccc</div>
<div id="3">ddd</div>
</div>
<script type="text/javascript">
function ChildChangeListener(){
this.lastlength=document.getElementById("test").childNodes.length;
this.start();
}
//间隔半秒监听移除事件
ChildChangeListener.prototype.start=function(){
this.listener();
}
ChildChangeListener.prototype.listener=function(){
var currlength=document.getElementById("test").childNodes.length;
var num1=this.lastlength+1
if(num1-currlength!=0){
alert(this.lastlength+","+currlength);
}
}
//间隔1秒移出一个节点
function remove(){
var test = document.getElementById("test");
if(test.childNodes.length>0){
test.removeChild(test.firstChild);
setTimeout("remove()",1000);
}
}
function add(){
var div=document.createElement("div");
div.appendChild(document.createTextNode("new div"));
var test = document.getElementById("test");
test.appendChild(div);
}
var li=new ChildChangeListener();
</script>
<input type="button" onclick="remove();" value="delete"></button>
<input type="button" onclick="add();" value="add"></button>[/code]
上面的2段代码在ChildChangeListener.prototype.start=function(){
this.listener();}
这里有区别
ChildChangeListener.prototype.start=function(){
setInterval(this.listener,500); }
为什么第一段代码,显示this.length是UNDERFIND
<div id="0">aaa</div>
<div id="1">bbb</div>
<div id="2">ccc</div>
<div id="3">ddd</div>
</div>
<script type="text/javascript">
function ChildChangeListener(){
this.lastlength=document.getElementById("test").childNodes.length;
this.start();
}
//间隔半秒监听移除事件
ChildChangeListener.prototype.start=function(){
setInterval(this.listener,500);
}
ChildChangeListener.prototype.listener=function(){
var ccl=this;
var currlength=document.getElementById("test").childNodes.length;
if(ccl.lastlength-currlength!=0){
alert(this.lastlength+","+currlength);
this.lastlength=currlength;
}
}
function startit(){
remove();
}
//间隔1秒移出一个节点
function remove(){
var test = document.getElementById("test");
if(test.childNodes.length>0){
test.removeChild(test.firstChild);
setTimeout("remove()",1000);
}
}
function add(){
var div=document.createElement("div");
div.innerHTML="new div";
var test = document.getElementById("test");
test.appendChild(div);
}
</script>
<script type="text/javascript">
var li=new ChildChangeListener();
</script>
<input type="button" onclick="startit();" value="delete"></button>
<input type="button" onclick="add();" value="add"></button>[/code][code]<div id="test">
<div id="0">aaa</div>
<div id="1">bbb</div>
<div id="2">ccc</div>
<div id="3">ddd</div>
</div>
<script type="text/javascript">
function ChildChangeListener(){
this.lastlength=document.getElementById("test").childNodes.length;
this.start();
}
//间隔半秒监听移除事件
ChildChangeListener.prototype.start=function(){
this.listener();
}
ChildChangeListener.prototype.listener=function(){
var currlength=document.getElementById("test").childNodes.length;
var num1=this.lastlength+1
if(num1-currlength!=0){
alert(this.lastlength+","+currlength);
}
}
//间隔1秒移出一个节点
function remove(){
var test = document.getElementById("test");
if(test.childNodes.length>0){
test.removeChild(test.firstChild);
setTimeout("remove()",1000);
}
}
function add(){
var div=document.createElement("div");
div.appendChild(document.createTextNode("new div"));
var test = document.getElementById("test");
test.appendChild(div);
}
var li=new ChildChangeListener();
</script>
<input type="button" onclick="remove();" value="delete"></button>
<input type="button" onclick="add();" value="add"></button>[/code]
上面的2段代码在ChildChangeListener.prototype.start=function(){
this.listener();}
这里有区别
ChildChangeListener.prototype.start=function(){
setInterval(this.listener,500); }
为什么第一段代码,显示this.length是UNDERFIND
为什么第一段代码,显示this.length是UNDERFIND
re:
对象未定义或未初始化
this.length== undefined,已定义,未赋值
typeof(this.length)== undefined,未定义