1、動態綁定事件:
<div?id=box></div><button?id=btn>動態生成標簽</button>
<script>
window.onload=function(){
document.getElementById("btn").onclick=function(){
var?span=document.createElement("span"); span.innerHTML="點我"+Math.random(); span.onclick=function(){ alert(this.innerHTML); } document.getElementById("box").appendChild(span);}
}
</script>
2、事件委托:
<div?id=box></div><button?id=btn>動態生成標簽</button>
<script>
window.onload=function(){
document.getElementById("box").onclick=function(e){
var?e=e||window.event; if(e.target!=e.currentTarget){ alert(e.target.innerHTML); }}
document.getElementById("btn").onclick=function(){
var?span=document.createElement("span"); span.innerHTML="點我"+Math.random(); document.getElementById("box").appendChild(span);}
}
</script>