javascript - jQuery tablesorter 子元素禁用排序

标签 javascript jquery html tablesorter

我在 th 中有子元素,它们是弹出窗口。当我单击 .show_history div 以显示弹出式 div .show_history_ctn 时,将触发对该列的排序。我已将 .show_history 的 z-index 增加到 9999,但仍会触发排序。我还在 .show_history 单击事件中添加了 stopPropagation,并且仍然会进行排序。

jQuery

$(".show_history").on("click",function(event) {
        $(this).siblings(".show_history_ctn").find("tr").show();
        event.stopPropagation();
        if($(this).hasClass("active")) {
            $(this).siblings(".show_history_ctn").slideUp();
            $(this).removeClass("active");
        } else {
            $(".show_history_ctn").hide();
            $(".show_history").removeClass("active");
            $(this).siblings(".show_history_ctn").slideDown();
            $(this).addClass("active");
        }
    });

$(".tablesorter").tablesorter();

html

<table class='tablesorter'><thead><tr><th><div class='show_history'>Show History</div><div class='show_history_ctn' style='display:none'>**content**</div></th><th></th></tr></thead></table>

我该如何解决?我需要对列进行排序,否则我只需添加 sorter:'false'

最佳答案

问题是 tablesorter 删除了点击绑定(bind),因为它重建了表头。您可以使用以下任一方法解决此问题:

  • headerTemplate 选项设置为空字符串 ("") - 这可以防止更改 header 内容,因此不会破坏任何绑定(bind)。在内部,它使用 innerHTML(这可能很快就会改变)来包装内容,因为 jQuery 的包装在旧版本的 IE 中非常慢。
  • initialized 回调 ( demo ) 中绑定(bind)弹出链接

    $(function() {
    
      function bindLink() {
        $('.link').click(function(e) {
          e.preventDefault();
          e.stopPropagation();
        });
      }
    
      $('table').tablesorter({
        theme: 'blue',
        initialized: bindLink
      });
    
    });
    

更新:糟糕,我忘了包含 cssNoSort class name需要将“tablesorter-noSort”添加到您单击的元素中。上面更新了演示链接。

<th>AlphaNumeric <a class="link tablesorter-noSort" href="https://google.com">test</a>

关于javascript - jQuery tablesorter 子元素禁用排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46811339/

相关文章:

javascript - AJAX 返回和提交 HTML

javascript - 如何在 Meteor 中从客户端向服务器发出简单的 http 请求

javascript - 将动态元素放在行上,占用所有可用空间,而不会跨多行打断组

javascript - 单击标题内的特定元素时如何禁用 Bootstrap 折叠?

多个元素上的 JavaScript 单击事件监听器并获取目标 'rel' 属性

javascript - 在对象字面量中缓存元素时使用 find() 的语法

JavaScript:Object.method 与 Object.prototype.myMethod?

jquery - 将 href 添加到所选区域的 div

javascript - 使整个 div 可点击(调用函数)

java - 我如何从 html 中获取细节?