【问题标题】:Unable to get the newly created table cell through jquery无法通过jquery获取新创建的表格单元格
【发布时间】:2010-11-20 17:58:33
【问题描述】:

我遇到了一个问题,我正在使用 jquery 创建一个 html 表格并将点击事件添加到它的表格单元格中。它工作得很好,但是当我在该表中添加一个新的表行时,它不会捕获新创建的行上的点击事件。前面几行工作正常。

我将向您发送我的代码,我们将非常感谢您的帮助。

 <style type="text/css">
    .cpHeader
    {
        color: white;
        background-color: #719DDB;
        font: bold 11px auto "Trebuchet MS", Verdana;
        font-size: 12px;
        cursor: pointer;
        width:450px;
        height:18px;
        padding: 4px;           
    }
    .cpBody
    {
        background-color: #DCE4F9;
        font: normal 11px auto Verdana, Arial;
        border: 1px gray;               
        width:450px;
        padding: 4px;
        padding-top: 7px;

    } 
    .imageClass
    {
        background-image:url(Images/expand.png);

    }
    .tableCell
    {
       padding: 5px;
    }
     .buttonCSS
     {
        width:50px;
        height:50px;
        background-color:Green;


     }     
</style>

<script type="text/javascript">
   $(document).ready(function () {
        createTable($("#tbl"), 5, 5);

        $(".tableCell").click(function () {
            alert("Column # = " + $(this).data("col") + "\n Row # = " + $(this).data("row"));

        });

        function createTable(tbody, rows, column) {
            if (tbody == null || tbody.length < 1) {
                return;
            }
            for (var i = 0; i < rows; i++) {
                var trow = $("<tr>");
                trow.data("trow", i);
                for (var j = 0; j < column; j++) {
                    var celltext = "" + i + "." + j;
                    $("<td>")
                        .addClass("tableCell")
                        .text(celltext)
                        .data("col", j + 1)
                        .data("row", i + 1)
                        .appendTo(trow);
                }
                trow.appendTo(tbody);
            }
            $(tbody).data("rows", rows);
            $(tbody).data("cols", column);

        }

        $("#addRowBtn").click(function () {
            var table = $("#tbl");
            var trow = $("<tr>");
            var columns = table.data("cols");
            for (var i = 0; i < columns; i++) {
                var td = $("<td>")
                td.addClass("tableCell");
                td.data("col", i + 1)
                td.data("row", table.data("rows") + 1 )
                td.text("" + (table.data("rows") + 1) + "." + (i + 1))
                td.appendTo(trow);
            }
            trow.appendTo(table);
            table.data("rows", table.data("rows") + 1);
        });

    });

</script>


【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    您应该使用delegate 函数在此处绑定您的事件处理程序。

    $('#tbl').delegate('.tableCell', 'click', function(){
        alert("Column # = " + $(this).data("col") + "\n Row # = " + $(this).data("row"));
    });
    

    这将捕获所有对.tableCells 的点击,这些点击是#tbl 的子元素,即使在调用delegate.tableCell 元素不存在。

    这是一个简单的现场演示:http://www.jsfiddle.net/yijiang/hBkKh/

    【讨论】:

    • 非常感谢,但有点困惑的是,即使没有设置委托,当早期的单元正在工作时,为什么我必须为新创建的单元添加委托?
    • @Abdul 你不这样做,你应该使用这个而不是点击事件处理程序——这也适用于你现有的单元格。如果您想详细了解delegatebind 之间的区别,可以查看我在答案中链接到的文档,或者搜索比我更有经验的人提供的出色解释在这里
    猜你喜欢
    • 2018-06-24
    • 2020-01-10
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多