0

我想在单击图像时拨打一个号码。为此,我动态创建了包含此类图像的 div 标签。但我每次都需要双击图像才能拨打电话。我认为它正在发生,因为在第一次单击时调用了 wrap 函数,而在第二次单击时调用了 tel 。我想在第一次点击时拨打电话。请为此提出替代方案。我的代码是:

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+"/>");
        }
    }
});
4

1 回答 1

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");
    }
于 2012-08-01T13:27:46.657 回答