2012-08-01 42 views
0

我想拨一个点击图片若干。对于这个我动态创建含有这种图像div标签。但我需要每次双击图像才能拨打电话。 我认为这是因为第一次点击wrap功能被调用,第二次点击tel被调用。我想先打电话,请为此提出一个替代方案。 我的代码是:避免包装函数内双击在HTML

for(var i=0;i<len;i++) 
    { 
     var maindiv=document.createElement("div"); 
     maindiv.setAttribute("id", "maintablediv"); 
     var table=document.createElement("table"); 
     table.setAttribute("class","numbertable"); 
     table.setAttribute("border",0); 
     table.setAttribute("cellSpacing","0"); 
     table.setAttribute("cellPadding","0"); 
     var rowCount = table.rows.length ; 
     var row =table.insertRow(rowCount); 
     row.id="row"; 
     var cell1 = row.insertCell(0); 
     if(i==0) 
     { 
      cell1.setAttribute("id","cmdcentralno"); 
     } 
     var cell2=row.insertCell(1); 
     var p=document.createElement("p"); 
     p.innerHTML=desc[i]; 
     cell1.innerHTML=num[i]; 
     cell1.setAttribute("class","numbername"); 
     cell1.appendChild(document.createElement("br")); 
     cell1.appendChild(p); 
     cell2.setAttribute("class","phoneImg"); 
     var image=document.createElement("img"); 
     image.setAttribute("src", "images/Icon-Phone-Green.png"); 
     if(i==0) 
     { 
      cell2.setAttribute("id","cmdcentralimage"); 
     } 
     image.setAttribute("id", "imgphn"+i); 

     image.className="imgphn"; 

     // var number=num[i].substring(num[i].indexOf('>')+1,num[i].indexOf('>')+14); 

     image.setAttribute("name", dialnum[i]); 
     cell2.appendChild(image); 
     image.setAttribute("align","centre"); 
     maindiv.appendChild(table); 
     document.getElementById("numberMenu").appendChild(maindiv); 
     image.onclick=function() 
     { 
      var cellnumber=$(this).attr("name"); 
      $(this).wrap("<a href =tel:"+cellnumber+"/>"); 
     } 
    } 
}); 

回答

0

未经检验的,但我希望这应该解决您的问题...

// move this outside of the click handler (and use `image` instead of `this`) 
    image.wrap("<a href =tel:"+cellnumber+"/>"); 

    image.onclick=function() 
    { 
     var cellnumber=$(this).attr("name"); 
    }