触发单击事件的jQuery似乎落后了一步

触发单击事件的jQuery似乎落后了一步,jquery,jquery-plugins,dhtml,tablesorter,Jquery,Jquery Plugins,Dhtml,Tablesorter,我正在使用tablesorter插件,这很好。我必须克隆原始标题,然后在可滚动区域上方重新插入克隆 要在隐藏的旧表头元素上启动tablesorter插件,当用户单击可见的克隆表时,我会在隐藏表上触发click using.trigger 以下是jQuery: $('.w_price_assess').delegate('#quotations_clone thead th', 'click', function() { var $cloneTh = $(this);

我正在使用tablesorter插件,这很好。我必须克隆原始标题,然后在可滚动区域上方重新插入克隆

要在隐藏的旧表头元素上启动tablesorter插件,当用户单击可见的克隆表时,我会在隐藏表上触发click using.trigger

以下是jQuery:

    $('.w_price_assess').delegate('#quotations_clone thead th', 'click', function() {

        var $cloneTh = $(this);
        var $cloneThIndex = $cloneTh.index('#quotations_clone thead th');

        $('#quotations thead th:eq(' + $cloneThIndex + ')').trigger('click');

        var $classList =$('#quotations thead th:eq(' + $cloneThIndex + ')').attr('class').split(/\s+/);            

        console.log($classList);

        $.each($classList, function(index, item){
            if (item==='headerSortDown') {
               $('#quotations_clone thead th').removeClass('headerSortUp headerSortDown'); 
               $('#quotations_clone thead th:eq(' + $cloneThIndex + ')').addClass('headerSortDown');
            } else if (item==='headerSortUp') {
                $('#quotations_clone thead th').removeClass('headerSortUp headerSortDown');
                $('#quotations_clone thead th:eq(' + $cloneThIndex + ')').addClass('headerSortUp');
            } else {
                //$('#quotations_clone thead th').removeClass('headerSortUp headerSortDown');
            }
        });    

    });
出现的问题是,当我第一次单击克隆的th元素时,它不会立即返回table sorter附加到隐藏的th元素的类


我需要它同时注册,但不确定如何进行?

我通过使用如下全局变量来解决此问题:

    var $orginalHead = $("#tablesorter-demo thead");

    globalIndex = null;

    $($orginalHead)
    .clone()
    .insertBefore("#tablesorter-demo")
    .wrap('<table id="sorter-clone" />');

    $("body").delegate("#sorter-clone thead th", "click", function() {
        var $tHeader = $(this);
        $tHeaderIndex = $tHeader.index("#sorter-clone thead th");
        $('#tablesorter-demo thead th:eq(' + $tHeaderIndex + ')').trigger('click'); 
    });

    $("#tablesorter-demo").bind("sortEnd", function() {
        var $classList = $('#tablesorter-demo thead th:eq(' + $tHeaderIndex + ')').attr('class').split(/\s+/);
        replaceClasses($classList, $tHeaderIndex, globalIndex);                 
    });

    function replaceClasses(classes, index, oldIndex) {

        globalIndex = index;
        var list = classes.toString().replace(",", " ");
        var oldHead = $("#sorter-clone thead th:eq(" + oldIndex + ")").removeAttr("class");
        var newHead = $("#sorter-clone thead th:eq(" + index + ")").removeAttr("class").addClass(list);         

        if (oldIndex !== null) {
            return {x:oldHead, y:newHead};
        } else {
            console.log("bar");
        }
    }

基本上,我需要在点击触发后更新克隆表上的类,因为它们直到点击触发后才被添加。