如何向HTML元素动态添加事件?
在项目中使用extremecomponents显示数据,在一个实例中,由于用到标签AutoGenerateColumns生成数据,结果需要对单元格td添加onclick事件,只能通过动态向document中的td添加Event: function addEvent() { col=document.getElementsByTagName("td");//获取所有的td标签 for(i=0; i<col.length; i++) { whichEl=col[i];//取出单个标签 if(whichEl.className=="contenttitle")//本例中取出所有class="contenttitle"的td { whichEl.onclick=function()//td添加onclick事件 { alert(this.innerText);//使用this对象操作 . ........ } } } } 然后在<bady>中添加addEvent() <body onload="addEvent();">