【问题标题】:How can i unset the changes made by jquery?如何取消设置 jquery 所做的更改?
【发布时间】:2023-03-28 14:25:01
【问题描述】:

我正在根据第一个字符过滤数据,但是当用户单击全部时,我想显示所有数据,但是一旦我根据字符过滤了数据,我就无法将其重置为全部。

我想允许用户根据字母用户选择过滤数据,当用户点击所有用户应该能够再次看到所有项目。

我的代码:

$('.myFilters li').on("click", function() {
    if($(this).text() == "all"){
        $(this).show();
        return ;
    }
    var letter = $(this).text()[0];

    $('#mycatouter div').each(function() {
    letter=letter.toUpperCase();
    if ($(this).text()[0] == letter) {
      $(this).show();
    } else {
      $(this).hide();
    }
    });
});

【问题讨论】:

  • 插入你的html和css

标签: javascript jquery filtering


【解决方案1】:

问题在于 this 关键字的使用:

$('.myFilters li').on("click", function() {
    if($(this).text() == "all"){
        $('#mycatouter div').show();
        return;
    }
    var letter = $(this).text()[0];

    $('#mycatouter div').each(function() {
      letter = letter.toUpperCase();
      if ($(this).text()[0] == letter) {
        $(this).show();
      } else {
        $(this).hide();
      }
    });
});

在您的第一个 if 中,this 指的是事件目标,而在 each 循环中它指的是循环中的当前元素。

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多