无法使用dom连接到一行

时间:2012-02-02 05:31:29

标签: javascript dom

function addRow(names,count) {

    deleteRow();
    var table = dataTable;

    for(var i=0;i<count;i++)
    {

         var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell2 = row.insertCell(0);
        cell2.innerHTML = rowCount;

        var cell3 = row.insertCell(1);
        var element2 = document.createElement('tr');
        var element3 = document.createElement('td');
        //element2.type = "text";
        element2.appendChild(element3)
        cell3.appendChild(element2);
        cell3.innerHTML = names[i];
        element3.attachEvent("test()", onclick);

    }
}

这是我的代码示例,我无法将事件附加到element2或element3, 任何人都可以帮我解决这个问题

这是我尝试过的另一个示例,它包含完整的代码..

<HTML>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <SCRIPT language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            cell2.innerHTML = rowCount + 1;

            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "text";
            cell3.appendChild(element2);
            element2.attachEvent("onclick",function(){ alert("event triggered"); });


        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    var index = table.rowIndex;
                    alert("index"+index);
                    //table.deleteRow(i);
                    //rowCount--;
                    i--;
                }

            }
            }catch(e) {
                alert(e);
            }
        }

    </SCRIPT>
</HEAD>
<BODY>

    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

    <TABLE id="dataTable" width="350px" border="1">
        <TR>
            <TD><INPUT type="checkbox" name="chk"/></TD>
            <TD> 1 </TD>
            <TD> <INPUT type="text" /> </TD>
        </TR>
    </TABLE>

</BODY>
</HTML>

2 个答案:

答案 0 :(得分:1)

逻辑和语法错误,继承人崩溃。

cell3.appendChild(element2); // appends your element
cell3.innerHTML = names[i]; // deletes element2 and replaces it with names[i]

element2不存在,因为您在添加cell3后将names[i]内的所有内容设置为element2

您还需要更新事件绑定(IE)

element3.attachEvent("onclick", function(){ /* code */ });

答案 1 :(得分:0)

试试这个..

 element3.onclick = function () {
               test();
            }