當前位置:編程學習大全網 - 網站源碼 - jquery 給動態生成的標簽綁定事件的幾種方

jquery 給動態生成的標簽綁定事件的幾種方

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>

  • 上一篇:零售行業數據分析的常見指標,以可視化圖表展示?
  • 下一篇:在unity5.0中做賽車遊戲這個句子怎麽替換rigidbody.centerOfMass.x = COMX;?
  • copyright 2024編程學習大全網