我正在构建一个控件,我正在构建一个包含一个可滚动的主体的表。这些行有一个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>');
虽然成功添加了行,但由于某种原因,静态行与单击处理程序一起使用,但新添加的行不会。有什么我想念的吗?
答案 0 :(得分:3)
尝试使用.live():
$('.innerTable tr').live('click', function (e) { ... });
它会将事件处理程序绑定到与添加到DOM的选择器匹配的任何当前和 future 元素。