简体   繁体   English

实现可折叠:如何确定哪个部分是打开的?

[英]materialize collapsible: how to determine which section is open?

I have a materialize collapsible which works as expected. 我有一个可实现预期的可折叠材料。 Something similar to: 类似于:

<ul class="collapsible">
  <li>
   <div class="collapsible-header">Title1</div>
   <div class="collapsible-body" />
  </li>
  <li>
   <div class="collapsible-header">Title2</div>
   <div class="collapsible-body" />
  </li>
</ul>

In a later process, when pressing a button I need a javascript function to modify its behavior depending on which section is open. 在以后的过程中,当按下按钮时,我需要一个javascript函数来根据打开的部分来修改其行为。

How can I determine which section is open? 如何确定哪个部分是打开的?

I guess one possibility would be to store in a hidden element the index of the section when it is selected but I don't know how to do it. 我想一种可能是将选定部分的索引存储在隐藏元素中,但我不知道该怎么做。

Materializecss add an active class to an open collapsible item by itself. Materializecss本身向打开的可折叠项添加了active类。 So you can use it to understand which collapsible item is open. 因此,您可以使用它来了解哪个可折叠项目处于打开状态。

You can use this jquery code : 您可以使用以下jquery代码:

$(document).on("click","ul.collapsible li", function(){

  var elem = document.querySelectorAll("ul.collapsible li");
  var index = "none"

  for (var i = 0; i < elem.length; i++) {
    if (elem[i].className == "active") {
        index = i;
    }
    document.getElementById("show").innerHTML = index;
  }

})

This code show index of opened collapsible item for you. 此代码为您显示已打开的可折叠项目的索引。

Here is complete example : jsFiddle 这是完整的示例: jsFiddle

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

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