排序表后不会触发jQuery click()事件 [英] jQuery click() event isn't fired after sorting table

查看:103
本文介绍了排序表后不会触发jQuery click()事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你加载页面时,如果你点击一行,它会记录到控制台点击

When you load the page, if you click on a line, it logs to the console clicked.

但是,如果您对表进行排序(单击表标题),则单击上的事件tr 如果您尝试单击行,则不会被触发。

But, if you sort the table (click on the table header), click event on tr doesn't get fired if you try to click on the rows.

我正在使用这个插件: tablefixedheader

I'm using this plugin: tablefixedheader

jQuery

jQuery

$(document).ready(function(){
    $("table.ex").fixheadertable({
        caption        : 'Tarefas Disponíveis',
        showhide    : false,
        height        : '265',
        zebra       : true,
        zebraClass  : 'ui-state-default',
        sortable    : true,
        sortedColId : 0,
        dateFormat  : 'Y/m/d',
        pager        : true,
        rowsPerPage    : 25,
        colratio    : [110,150],
        minColWidth : 110,
        resizeCol    : true
    });

    // problem with this click
    // The event isn't triggered:

    $("table.ex tr").click(function(){
        console.log('clicked');
    });

});

DEMO http://jsfiddle.net/QpU3c/

推荐答案

你应该使用事件委托,因为该插件更改了原始 tr 元素,因此它们会丢失附加的事件处理程序。处理程序应该附加在表本身上,因为它肯定没有改变。

You should use event delegation, because the plugin changes the original tr elements, so they lose their attached event handlers. The handler should be attached on the table itself, because it is certainly not changing.

$("table.ex").on('click', 'tr', function(){
    console.log('clicked');
});

jsFiddle演示

这篇关于排序表后不会触发jQuery click()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆