学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
轻松建立自己的群组,招兵买马   
发新话题
打印

javascript动态创建控件事件的方法

javascript动态创建控件事件的方法

function commback(msg){
   var msglength=msg.length;
    var p=document.getElementById("textinfo");
    for(var i=0;i<msglength;i++){
              var str=msg[i];
               var communicationId=str.substring(0,str.lastIndexOf("*"));
          var content=str.substring(str.lastIndexOf("*")+1,str.lastIndexOf("#"));
          var  publishDate=str.substring(str.lastIndexOf("#")+1,str.lastIndexOf("@"));
           var   recorde=str.substring(str.lastIndexOf("@")+1,str.length);
            var con;
            if(content.length>6){
              content=content.substring(0,6);
              con=" "+content+"......   ";
            }else{
               con=" "+content+"......   ";
            }
            a=document.createElement("a");
             a.setAttribute("id",communicationId);
              //a.setAttribute("href","#");
               a.setAttribute("color","#00ff0"+communicationId);
               a.setAttribute("name","avalue");
              a.onclick=new Function("newOpen(communicationId,content,publishDate,recorde)");

             a.appendChild(document.createTextNode(con));
             p.appendChild(a);    
        }
}
function newOpen(communicationId,content,publishDate,recorde){
    Ext.getCmp("recorde").setValue(recorde);
    Ext.getCmp("publishDate").setValue(publishDate);
    Ext.getCmp("content").setValue(content);
    win.show();
   
}
html中是这个样子的: <p id="textinfo" style="cursor:hand"></p>
我在标签p中动态添加标签<a>,msg是一个数组,有几个数据就创建几个标签a,然后在每个标签上动态创建onclick方法,可是当我触发点击事件的时候,总是msg的返回最后一条数据,应该要实现的效果是,点击那个标签就返回那条记录,谢谢高手们看看,帮我解决

TOP

……
a.onclick=createLinstener(communicationId,content,publishDate,recorde);
……

function createLinstener(communicationId,content,publishDate,recorde) {
    return function() {
         newOpen.call(this, communicationId,content,publishDate,recorde);
    }
}

ps:出现问题的原因:Functiond的实例导致产生闭包

[ 本帖最后由 渚薰 于 2008-5-26 10:23 编辑 ]
个人ajax技术专题站: http://www.ajaxgo.org 我不会闲你烦,只会闲你不够烦!

TOP

发新话题