简体   繁体   English

隐藏并显示具有无序列表的div

[英]hide and show div with unordered list

I try to hide and show a div (filter-panel--content) with an unordered list in it, when clicking on the label above. 单击上面的标签时,我尝试隐藏并显示其中具有无序列表的div(过滤器面板-内容)。

#LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE
<div class="filter-panel--flyout">
   <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

#DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED
<div class="filter-panel--content">
   <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
      <div class="option--container">
         <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__575" name="__f__575" value="575">
         </span>
      <label class="filter-panel--label" for="__f__575">8</label>
      </div>
      </li>
   </ul>
</div></div>

#LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE
<div class="filter-panel--flyout">
   <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

#DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED
<div class="filter-panel--content">
   <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
      <div class="option--container">
         <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__576" name="__f__576" value="576">
         </span>
      <label class="filter-panel--label" for="__f__576">9</label>
      </div>
      </li>
   </ul>
</div>

#LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE
<div class="filter-panel--flyout">
   <div id="label-it" class="label-it"><label class="filter-panel--title">
      <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div>
   </label></div>

#DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED
<div class="filter-panel--content">
   <ul class="filter-panel--option-list">
      <li class="filter-panel--option">
      <div class="option--container">
         <span class="filter-panel--checkbox">
         <input type="checkbox" id="__f__577" name="__f__577" value="577">
         </span>
      <label class="filter-panel--label" for="__f__577">10</label>
      </div>
      </li>
   </ul>
</div>

Here is my click() function, which toggle's the class (changing a backgroundimage) and additionally i want to hide and show the clicked filter-panel--content: 这是我的click()函数,该函数切换类(更改backgroundimage),此外,我想隐藏并显示单击的滤镜面板内容:

$('.label-it').click(function() {
  $(this).find(".filter-panel--title div").toggleClass('klapp klappe');
  //hide and show filter-panel--content
  //tried something like this but it doesn't worked: 
  //$(this).find(".filter-panel--content div").hide();
});

But it should only closes the clicked label filter-panel--content, not every class of it. 但是,它只应关闭单击的标签filter-panel(内容),而不是关闭每个类别。 Can someone help? 有人可以帮忙吗? Thank you! 谢谢!

-- edit: -编辑:

i have a additional question, the toggle works fine $(this).closest(".filter-panel--flyout").find(".filter-panel--content div") but when i submit my inputs, the display:none classes will be refreshed and it is display:block.. how can i submit my toggled .filter-panel--content so that it stays toggled? 我还有一个问题,切换功能可以正常使用$(this).closest(".filter-panel--flyout").find(".filter-panel--content div")但是当我提交输入时,显示:none类将被刷新,它是display:block ..我如何提交已切换的.filter-panel--content使其保持切换状态?

You can't use $(this).find(".filter-panel--content div") because filter-panel--content is not a child of label-it . 您不能使用$(this).find(".filter-panel--content div")因为filter-panel--content不是label-it的子label-it

So in order to make it work, you have to use .closest() like as in $(this).closest(".filter-panel--flyout").find(".filter-panel--content div") 因此,为了使其正常工作,您必须像$(this).closest(".filter-panel--flyout").find(".filter-panel--content div") .closest()那样使用.closest() $(this).closest(".filter-panel--flyout").find(".filter-panel--content div")

Demo 演示版

 $('.label-it').click(function() { $(this).find(".filter-panel--title div").toggleClass('klapp klappe'); $(this).closest(".filter-panel--flyout").find(".filter-panel--content").toggle(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="filter-panel--flyout"> <div id="label-it" class="label-it"><label class="filter-panel--title"> <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div> </label> </div> #DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED <div class="filter-panel--content"> <ul class="filter-panel--option-list"> <li class="filter-panel--option"> <div class="option--container"> <span class="filter-panel--checkbox"> <input type="checkbox" id="__f__575" name="__f__575" value="575"> </span> <label class="filter-panel--label" for="__f__575">8</label> </div> </li> </ul> </div> </div> #LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE <div class="filter-panel--flyout"> <div id="label-it" class="label-it"><label class="filter-panel--title"> <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div> </label> </div> #DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED <div class="filter-panel--content"> <ul class="filter-panel--option-list"> <li class="filter-panel--option"> <div class="option--container"> <span class="filter-panel--checkbox"> <input type="checkbox" id="__f__576" name="__f__576" value="576"> </span> <label class="filter-panel--label" for="__f__576">9</label> </div> </li> </ul> </div> </div> #LABEL/DIV WHICH CAN BE CLICKED AND THE .click() SHOULD FIRE <div class="filter-panel--flyout"> <div id="label-it" class="label-it"><label class="filter-panel--title"> <h3 class="rums">Ports</h3><div id="klapp" class="klapp"></div> </label></div> #DIV WHICH SHOULD show AND hide WHEN LABEL IS CLICKED <div class="filter-panel--content"> <ul class="filter-panel--option-list"> <li class="filter-panel--option"> <div class="option--container"> <span class="filter-panel--checkbox"> <input type="checkbox" id="__f__577" name="__f__577" value="577"> </span> <label class="filter-panel--label" for="__f__577">10</label> </div> </li> </ul> </div> </div> 

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

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