如何优化我的jQuery代码,以便缩小范围并显示(过滤)特定内容

约翰

有什么方法可以优化我的代码,以便在过滤时可以获得更精确的结果?例如,当您在PC上检查RTS(战略游戏)时,想要获得PC的战略游戏(您应该获得StarCraft),但同时获得PC和Strategy的所有游戏,因此,这只是添加到游戏库中而不会缩小向下...在此先感谢您的任何投入。

有人可以提供一些有关如何优化我的代码的指示,以便在过滤时可以获得更精确的结果吗?例如,当您在PC上检查RTS(战略游戏)时,想要获得PC的战略游戏(您应该获得StarCraft),但同时获得PC和Strategy的所有游戏,因此,这只是添加到游戏库中而不会缩小向下...在此先感谢您的任何投入。

http://codepen.io/anon/pen/azaoPj

<div id="container">

  <div id="filter">
    <h3>Filter By</h3>
    <div class="category-filters"><img id="reset" src="http://www.asus.com/media/images/rest.png" alt="" />Reset</div>
    <div class="category-filters">
      <div class="genre">
        <img src="http://www.asus.com/media/images/close_round.png" id="open" class="col"/> Genre
      </div>

      <div id="genre-filters">
        <ul  class="filter-options">
          <li><input type="checkbox" value="filter_action" data-filter_id="action"> Action</li>
          <li><input type="checkbox" value="filter_fps" data-filter_id="fps"> FPS</li>
          <li><input type="checkbox" value="filter_rpg" data-filter_id="rpg"> RPG<li>
          <li><input type="checkbox" value="filter_rts" data-filter_id="rts"> RTS</li>
          <li><input type="checkbox" value="filter_adventure" data-filter_id="adventure"> Adventure</li>
        </ul>
      </div> 
    </div>

    <div class="category-filters">
      <div class="brand">
        <img src="http://www.asus.com/media/images/close_round.png" id="open" class="col1"/> Platform
      </div>

      <div id="brand-filters" class="category-filters">
        <ul  class="filter-options">
          <li><input type="checkbox" value="filter_pc" data-filter_id="pc"> PC</li>
          <li><input type="checkbox" value="filter_ps3" data-filter_id="ps3"> PS3</li>
          <li><input type="checkbox" value="filter_ps4" data-filter_id="ps4"> PS4<li>
          <li><input type="checkbox" value="filter_x360" data-filter_id="x360"> Xbox 360</li>
          <li><input type="checkbox" value="filter_xone" data-filter_id="xone"> Xbox one</li>
        </ul>
      </div> 
    </div>

  </div>


  <div id="products">
    <ul id="product-list">

      <li class="filter_pc filter_fps filter_xone">
        <img src="https://encrypted-tbn0.gstatic.com/shopping?q=tbn:ANd9GcRq1K6i1syrOr20l1k43vcJCSjeMHJP7c0w-xS-xmpSUPM1EuGGLPNI73gox8cbJX9k0tlRtvfm&usqp=CAE" alt="" />
      </li>
      <li class="filter_action filter_adventure filter_pc filter_ps3 filter_x360">
        <img src="http://s4.thcdn.com/productimg/0/300/300/52/10780752-1370965433-955769.jpg" alt="" />  
      </li>
      <li class="filter_fps filter_pc filter_ps3 filter_x360">
        <img src="http://s2.thcdn.com/productimg/0/300/300/42/10846642-1389618973-536407.jpg" alt="" />
      </li>  
      <li class="filter_rts filter_pc">
        <img src="http://s2.thcdn.com/productimg/0/300/300/01/10654001-1353502110-933610.jpg" alt="" />
      </li>
      <li class="filter_rpg filter_ps4">
        <img src="http://s2.thcdn.com/productimg/0/180/180/92/10968092-1422879679-941767.jpg" alt="" />
      </li>
    </ul> 

  </div>

</div>

jQuery的

$(document).ready(function () {

  $(".filter-options :checkbox").click(function(){
    $("#product-list li").hide();
    $(".filter-options :checkbox:checked").each(function(){
      $("." + $(this).val()).fadeIn();
    });
    if ($('.filter-options :checkbox').filter(':checked').length == 0 ) {
      $("#product-list li").fadeIn();
    }
  });
});
偶像

所以,此刻的你在一个类中的所有物品褪色等于.val()任何的复选框。您真正想要做的是将选择器加在一起,以获得一个选择器,该选择器仅选择具有所有所需类名的元素。

在CSS中,这真的很容易,您需要的是这样的选择器:

.filter_pc.filter_fps

因此,我们需要使用jQuery通过将各个值加在一起来创建它,如下所示:

var search = '';
$(".filter-options :checkbox:checked").each(function(){
  search += '.' + $(this).val(); // Add each val to the current search
});
// By this stage search will be equal to '.filter_pc.filter_fps'
// Now do the search
$(search).fadeIn();

因此,对于每个项目,您都将其连接起来,并在每个类名的前面加上一个点。然后,CSS选择器将专门引用具有所有这些类的项目。

这是一个演示:http : //codepen.io/EightArmsHQ/pen/ogPjwv

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有一个从 SQL Server 中提取的 PHP 表来显示表数据。我想向列添加列标题过滤器以缩小范围

来自分类Dev

运行Ansible剧本时,如何缩小范围?

来自分类Dev

使用C ++ lock_guard时如何缩小范围?

来自分类Dev

此代码如何优化缩小?

来自分类Dev

如何优化我的代码,以便不使用循环

来自分类Dev

我如何优化我的Jquery代码?

来自分类Dev

我如何优化代码

来自分类Dev

我如何优化下面的代码,以便它可以接受任何数组大小并给我结果

来自分类Dev

最小范围的jQuery移动范围滑块

来自分类Dev

如何优化/加速此代码,以便我可以处理大数据集?

来自分类Dev

以numpy生成缩小范围的一维数组

来自分类Dev

如何优化我的AVX代码

来自分类Dev

如何优化.filter函数,以便更快地完成过滤?

来自分类Dev

如何优化.filter函数,以便更快地完成过滤?

来自分类Dev

如何过滤我的tableview的内容?

来自分类Dev

如何在Oracle中找到两个大小范围之间缺少的大小范围?

来自分类Dev

SSL(imap)在某些计算机上不起作用-SSL握手失败-如何进一步缩小范围?

来自分类Dev

VBA-代码优化,可多次复制特定范围的列

来自分类Dev

范围选择的优化代码

来自分类Dev

如何将范围集合减少到最小范围

来自分类Dev

我如何重新排列表格显示,以便每个 th 都高于它的特定 td?

来自分类Dev

jQuery Ui范围滑块内容显示在步骤

来自分类Dev

jQuery Ui范围滑块内容显示在步骤

来自分类Dev

我如何在jQuery中显示<p>的内容

来自分类Dev

查找和访问特定的DIV,以便我可以附加内容

来自分类Dev

我如何优化以下代码?

来自分类Dev

我如何优化此Codewars C ++代码?

来自分类Dev

我如何优化此Java代码?

来自分类Dev

如何查找大小范围内的文件?

Related 相关文章

  1. 1

    有一个从 SQL Server 中提取的 PHP 表来显示表数据。我想向列添加列标题过滤器以缩小范围

  2. 2

    运行Ansible剧本时,如何缩小范围?

  3. 3

    使用C ++ lock_guard时如何缩小范围?

  4. 4

    此代码如何优化缩小?

  5. 5

    如何优化我的代码,以便不使用循环

  6. 6

    我如何优化我的Jquery代码?

  7. 7

    我如何优化代码

  8. 8

    我如何优化下面的代码,以便它可以接受任何数组大小并给我结果

  9. 9

    最小范围的jQuery移动范围滑块

  10. 10

    如何优化/加速此代码,以便我可以处理大数据集?

  11. 11

    以numpy生成缩小范围的一维数组

  12. 12

    如何优化我的AVX代码

  13. 13

    如何优化.filter函数,以便更快地完成过滤?

  14. 14

    如何优化.filter函数,以便更快地完成过滤?

  15. 15

    如何过滤我的tableview的内容?

  16. 16

    如何在Oracle中找到两个大小范围之间缺少的大小范围?

  17. 17

    SSL(imap)在某些计算机上不起作用-SSL握手失败-如何进一步缩小范围?

  18. 18

    VBA-代码优化,可多次复制特定范围的列

  19. 19

    范围选择的优化代码

  20. 20

    如何将范围集合减少到最小范围

  21. 21

    我如何重新排列表格显示,以便每个 th 都高于它的特定 td?

  22. 22

    jQuery Ui范围滑块内容显示在步骤

  23. 23

    jQuery Ui范围滑块内容显示在步骤

  24. 24

    我如何在jQuery中显示<p>的内容

  25. 25

    查找和访问特定的DIV,以便我可以附加内容

  26. 26

    我如何优化以下代码?

  27. 27

    我如何优化此Codewars C ++代码?

  28. 28

    我如何优化此Java代码?

  29. 29

    如何查找大小范围内的文件?

热门标签

归档