响应后在 table 单元格上添加双击处理程序

Add doubleclick handler on table cell after response

我想在 table 来自 servlet "downloaded" 并且 "inserted" 来自 javascript 之后将双击事件处理程序添加到 table 单元格。 我有一个 javascript 循环迭代 xml 对地图数据 ini table 的响应。表示一个cell可以

<td class='red' ></td>

我想在该单元格上添加一个功能,我尝试了几种解决方案,但 none works.One 是:

$(".red").on("dblclick",myfunction);

帮忙?

更新: Table 收到响应后的构造函数

function handleResponse(responseXML) {
    var i;
    var x=responseXML.getElementsByTagName("row");
    var out="<table><tr><th >Description</th><th >State</th><th>Note</th></tr>";
    for(i=0;i<x.length;i++){
        out+="<tr>";
    var Description = x[i].getElementsByTagName("Description")[0].childNodes[0].nodeValue;
    var State = x[i].getElementsByTagName("State")[0].childNodes[0].nodeValue;
    var note =  x[i].getElementsByTagName("Note")[0].childNodes[0];
    var note_text=" ";
    if (!(typeof note === "undefined") && !(note=='null')) {
        note_text=note.nodeValue;
    }
    out += "<td>"+Description+ "</td>";
    if(State==0)
        out+="<td class='white' ></td>";
    else if(State==1)
        out+="<td class='red' ></td>";
    else if(State==2)
        out+="<td class='yellow' ></td>";
    else if(State==3)
        out+="<td class='green' ></td>";

    out+="<td>" + note_text + "</td></tr>";
    }
    var output = document.getElementById("mytable");
    out+="</table>";
    $().on("click",".red",update());  //here is the point
    output.innerHTML=out;
}

更新:

根据您的评论,您可以试试这个:

打印时 td 稍微更改一下模板:

out+="<td class='red' ondblclick='openModal()'></td>";

然后在你的 js 文件中添加类似这样的内容。

$(document).ready(function() {
    function openModal() {
      alert( "Double clicked." );
      // Or call your other function here...
    };
});

您也可以通过多种方式完成此操作,请在此处查看:http://www.w3schools.com/jsref/event_ondblclick.asp