日期:2014-05-17  浏览次数:20961 次

动态按钮中如何控制onclick事件?
function   add()
{
var   f   =   document.getElementById( "avote ");
var   trObj   =   document.createElement( "TR ");
var   td1Obj   =   document.createElement( "TD ");
var   td2Obj   =   document.createElement( "TD ");
var   td3Obj   =   document.createElement( "TD ");
var   textObj   =   document.createElement( "INPUT ");
var   buttonObj   =   document.createElement( "A ");
ioption   +=   1;
textObj.type= "text ";
textObj.value= "选项 "+ioption;
textObj.name= "cont "+ioption;
textObj.maxlength= "250 ";
buttonObj.href   =   "# ";
buttonObj.id   =   ioption;
buttonObj.innerText   =   "清除 ";
buttonObj.onclick   =   resettext;
td3Obj.appendChild(textObj);
td3Obj.appendChild(buttonObj);
trObj.appendChild(td1Obj);
trObj.appendChild(td2Obj);
trObj.appendChild(td3Obj);
f.appendChild(trObj);
}

function   resettext()
{
}

Add是动态添加按钮时的函数,我想清除textObj文本框里的东西,怎么处理呢?

------解决方案--------------------
function add()
{
..;
textObj.name= "cont "+ioption;
textObj.id= "cont "+ioption;
...
}

function resettext()
{
var obj = event.srcElement;
if(obj.tagName== "A "){
var txtObj = eval( "document.getElementById( 'cont " + obj.id + " '); ");
txtObj.value = " ";
}
}