首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >触发单击事件的jQuery似乎落后了一步

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

Stack Overflow用户
提问于 2010-10-14 21:23:15
回答 1查看 482关注 0票数 0

我使用的是tablesorter插件,它很棒。我不得不克隆原始头,然后在可滚动区域上方重新插入克隆。

为了在隐藏的旧表头元素上触发tablesorter插件,当用户单击可见的克隆表时,我使用.trigger()在隐藏表上触发单击。

下面是jQuery:

代码语言:javascript
复制
    $('.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元素时,它不会立即返回表排序程序附加到隐藏的th元素的类。

我需要在同一时间注册,但不确定如何去做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-20 00:47:32

我设法通过使用全局变量解决了这个问题,如下所示:

代码语言:javascript
复制
    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");
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3933567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档