| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3832 人关注过本帖
标题:javascript动态创建控件事件的方法
只看楼主 加入收藏
zhoushuyan
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-12-6
收藏
 问题点数:0 回复次数:1 
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的返回最后一条数据,应该要实现的效果是,点击那个标签就返回那条记录,谢谢高手们看看,帮我解决
搜索更多相关主题的帖子: str var quot javascript 控件 
2008-05-22 16:31
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
收藏
得分:0 
……
 a.onclick=createLinstener(communicationId,content,publishDate,recorde);
……

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

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

[[it] 本帖最后由 渚薰 于 2008-5-26 10:23 编辑 [/it]]

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-05-26 10:22
快速回复:javascript动态创建控件事件的方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023886 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved