代码之家  ›  专栏  ›  技术社区  ›  Dave

jquery使用tr的click()处理程序向表中添加行

  •  3
  • Dave  · 技术社区  · 16 年前

    我正在构建的控件包含一个可滚动的表,它有一个问题。这些行建立了一个click()函数处理程序,如下所示:

        /**
         * This function is called when the user clicks the mouse on a row in
         * our scrolling table.
         */
        $('.innerTable tr').click (function (e) {
          //
          // Only react to the click if the mouse was clicked in the DIV or
          // the TD.
          //
          if (event.target.nodeName == 'DIV'  ||
              event.target.nodeName == 'TD'     ) {
            //
            // If the user wasn't holding down the control key, then deselect
            // any previously selected columns.
            //
            if (e.ctrlKey == false) {
              $('.innerTable tr').removeClass ('selected');
            }
    
            //
            // Toggle the selected state of the row that was clicked.
            //
            $(this).toggleClass ('selected');
          }
        });
    

    有一个按钮可以向表中添加如下行:

    $('#innerTable > tbody:last').append('<tr>...some information...</tr>');
    

    当成功添加行时,出于某种原因,静态行与单击处理程序一起工作,但新添加的行不工作。我有什么东西不见了吗?

    1 回复  |  直到 9 年前
        1
  •  3
  •   munch    16 年前

    尝试使用 .live() :

    $('.innerTable tr').live('click', function (e) { ... });
    

    它将事件处理程序绑定到任何当前和 未来 与添加到DOM的选择器匹配的元素。