功能需求:
   javascript动态创建一个table的行列,点击其中的单元格时,能响应一个click函数,并将当前cell的值作为入参传入。目前疑问:
  尝试了很多方法都不行,请教高手指点。下面是代码:
</html>
<script language = "javascript">
function RowClick(RowNum)
{
  alert("Rownum = "+RowNum);
}var rowIndex = 0;
function insertRow()
{
    var table = document.getElementById("MyTable");
    var objRow = table.insertRow();
    
    rowIndex = rowIndex+1;
    
    objRow.setAttribute("id",rowIndex);
    objRow.setAttribute("ondblclick",RowClick(rowIndex));
    
    var objCell0 = objRow.insertCell();
    objCell0.innerHTML = rowIndex;
    
    var objCell1 = objRow.insertCell();
    objCell1.innerHTML = rowIndex;
    
    var objCell2 = objRow.insertCell();
    objCell2.innerHTML = "<textarea id = 'newdesc' name ='newdesc'></textarea>";
    
}</script><body>
<table id = "MyTable" border = "1">
</table>
<input type = "button" value = "add" onclick = "insertRow()" /></html>上面的代码问题:
    点击添加按钮的时候相应函数,而不是点击单元格的时候响应。请高手赐教!!
急急急!!!!!!!!!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    试试objRow.ondblclick = function(){RowClick(this.id)};
      

  2.   

    <head runat="server">
        <title>无标题页</title>
        <style type="text/css"></style>    <script type="text/javascript" language="javascript" src="JQuery.js"></script>    <script src="a.js" id="sid1" defer="defer" type="text/javascript" language="javascript"></script>    <script language="javascript">
    function RowClick(RowNum)
    {
      alert("Rownum = "+RowNum);
    }var rowIndex = 0;
    function insertRow()
    {
      var table = document.getElementById("MyTable");
      var objRow = table.insertRow();
        
      rowIndex = rowIndex+1;
        
      objRow.setAttribute("id",rowIndex);
      //修改处
      objRow.attachEvent("onclick",insertRow);
        
      var objCell0 = objRow.insertCell();
      objCell0.innerHTML = rowIndex;
        
      var objCell1 = objRow.insertCell();
      objCell1.innerHTML = rowIndex;
        
      var objCell2 = objRow.insertCell();
      objCell2.innerHTML = "<textarea id = 'newdesc' name ='newdesc'></textarea>";
        
    }    </script></head>
    <body>
        <form id="form1" runat="server">
            <table id="MyTable" border="1">
            </table>
            <input type="button" value="add" onclick="insertRow()" />
        </form>
    </body>
    </html>
      

  3.   

    楼上ondblclick 是关机,楼主要的是单机
      

  4.   

    楼主你的需求到底是什么啊?
    点击其中的单元格时,能响应一个click函数
    点击添加按钮的时候相应函数,而不是点击单元格的时候响应
    貌似有点矛盾吧
      

  5.   

    function RowClick(RowNum)
    {
      alert("Rownum = "+RowNum);
    }var rowIndex = 0;
    function insertRow()
    {
      var table = document.getElementById("MyTable");
      var objRow = table.insertRow();
       
      rowIndex = rowIndex+1;
       
      objRow.setAttribute("id",rowIndex);
      objRow.setAttribute("ondblclick",RowClick(rowIndex));
       // 如果是行点击事件
     objRow.onclick = function(){RowClick(this.getAttribute("id"));};
      var objCell0 = objRow.insertCell();
      objCell0.innerHTML = rowIndex;
       
      var objCell1 = objRow.insertCell();
      objCell1.innerHTML = rowIndex;
       
      var objCell2 = objRow.insertCell();
      objCell2.innerHTML = "<textarea id = 'newdesc' name ='newdesc'></textarea>";
       // 如果是单元格点击事件
    for(var i=0;i<objRow.cells.length;i++){
    (function(i){objRow.cells[i].onclick=function(){cellCilck(this);}})(i);
    }
    }
    function cellCilck(cell){
    alert(cell.innerHTML);
    }