简体   繁体   English

使用 jQuery.each() 方法显示隐藏元素

[英]use jQuery.each() method to show hidden elements

I run a loop and create filters, which I pack in categories (packed in <ul> ).我运行一个循环并创建过滤器,我将它们打包在类别中(打包在<ul>中)。 These filters can be activated with checkboxes (packed in <li> within the <ul> ).这些过滤器可以用复选框激活(包装在 < <ul> <li>中)。 I can have 2 or 5 category filters, so 2 or 5 <ul> with multiple <li> inside each.我可以有 2 或 5 个类别过滤器,因此 2 或 5 个<ul>每个内部都有多个<li> With css all <li> checkboxes above 3 are hidden at start, and I use a "show more" element (in <span> ) that is located just before </ul> .使用 css 3 以上的所有<li>复选框在开始时都被隐藏,我使用位于</ul>之前的“显示更多”元素(在<span>中)。 As it's a loop, all <ul> elements use the same id="filterlist" and class="sidebar-ul".由于它是一个循环,所有<ul>元素都使用相同的 id="filterlist" 和 class="sidebar-ul"。 Also all the <li> checkboxes have the same class="sidebarfilters".此外,所有<li>复选框都具有相同的 class="sidebarfilters"。 I have a JQuery each function, and on click on one of the <span> I only want to show the hidden <li> elements, that are placed within the <ul> element, in which also the clicked span is located.我有一个 JQuery 每个 function,然后单击其中一个<span>我只想显示隐藏的<li>元素,这些元素放置在<ul>元素中,单击的跨度也位于其中。 At the moment, if I click on one of the "show more" spans, all <li> checkboxes of all <ul> are shown.目前,如果我单击“显示更多”跨度之一,则会显示所有<ul>的所有<li>复选框。 Is there a way (maybe with "$(this)") somehow to show only the correct <li> , even though classes are the same?有没有办法(也许用“$(this)”)以某种方式只显示正确的<li> ,即使类是相同的? Also, I want to hide the "show more" span if all <li> elements are shown, this is not working as well.另外,如果显示所有<li>元素,我想隐藏“显示更多”跨度,这也不起作用。 The <span> stays visible after all checkboxes are shown. <span>在显示所有复选框后保持可见。 Any input would be very much appreciated.任何输入将不胜感激。 My simplified code, CSS and JQuery are:我的简化代码 CSS 和 JQuery 是:

{% for tag in tags %}
  <ul  id="filterlist" class="sidebar-ul">
        {% if current_tags contains tag %} 
             <li class="sidebarfilters">
                 <input  class="filter_checkbox" id="checked" type="checkbox" checked onClick="">         
            </li>     
       <span class="showmorefilters">mehr...</span></ul>
  </ul> 
{% endfor %} 

 $(function() { $(".showmorefilters").each(function() { $(this).click(function() { $('#filterlist li:hidden').show(); if ($('#filterlist li').length == $('#filterlist li:visible').length) { $(this).hide(); } }); }); });
 .sidebarfilters:nth-child(n+5) { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sounds like content is being created dynamically.听起来内容是动态创建的。 Would advise using .on() versus .click() .建议使用.on() .click() Have to make a lot of assumptions without proper example.没有适当的例子就必须做出很多假设。

Consider the following.考虑以下。

$(function() {
  $("ul[id^='filterlist']").on("click", ".showmorefilters", function(e) {
    var fl = $(this).parent();
    fl.find("li:hidden").show();
    if ($('li', fl).length == $('li:visible', fl).length) {
      $(this).hide();
    }
  });
});

This will bind a Click event to all .showmorefilters elements and perform a callback that is relative to that target.这会将 Click 事件绑定到所有.showmorefilters元素并执行与该目标相关的回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM