如何防止手风琴关闭内容选择?

How to prevent an accordion from closing on content selection?

我的JS有问题,点击内容如何防止手风琴关闭

您可以在下面找到我的代码;代码工作正常;唯一的问题是当我 select 内容“下拉列表”中的任何内容时,手风琴关闭。如果我点击它,我需要手风琴保持打开状态。

我希望这是有道理的。

非常感谢任何帮助

    const accordion = document.getElementsByClassName ('media-box-actuaries_accordion');

for (i = 0; i<accordion.length; i++ ){
  accordion[i].addEventListener('click', function(){
    this.classList.toggle('active');

  })
}
.media-box-actuaries_accordion {
  position: relative;
  margin-bottom: 20px;
  text-decoration: none;
}

.media-box-actuaries_accordion {
  position: relative;
  margin-bottom: 20px;

}
.media-box-actuaries_accordion_content {
  position: relative;
  overflow: hidden;
  display: none;
  overflow-y: auto;
  transition: all 0.5s ease-out;
}

.media-box-actuaries_accordion.active .media-box-actuaries_accordion_content {
  height: 100%;
  display: block;
  transition: all 0.5s ease-out;
}

.media-box-actuaries .media-box-header {
  min-height: 69px;
  width: 100%;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: row;
  flex-direction: row;
  background-color: #f5f6f6;
  border: 1px solid #e2e2e2 !important;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);

}

/* accordion */
.media-box-actuaries .media-box-header-accordion {
  min-height: 69px;
  width: 100%;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: row;
  flex-direction: row;
  background-color: #f5f6f6;
  border: 1px solid #e2e2e2 !important;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.media-box-actuaries .media-box-header.only {
  padding: 0rem 2rem;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  justify-content: center;
  align-items: baseline;
  color: #00a3da;
  border-radius: 3px;
  border: 1px solid #e2e2e2;
}

.media-box-actuaries .media-box-header-accordion::before {
  content: " 04";
  position: absolute;
  right: 20px;
  transform: translateY(50%);
  font-size: 1.5em;
  color: #00a3da;
}

.media-box-actuaries .media-box-actuaries_accordion.active .media-box-header-accordion::before {
content: "03";
transform: translateY(100%);
font-size: 1.5em;
}

.media-box-actuaries .media-box-header-accordion.only {
  padding: 0rem 2rem;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  justify-content: center;
  align-items: baseline;
  color: #00a3da;
  border-radius: 3px;
  border: 1px solid #e2e2e2;
}

.media-box-actuaries .media-box-header-accordion__icon {
  min-width: 75px;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 3px 0px 0px 3px;
  border: 1px solid #e2e2e2;
  border-right: none;
}
    <div class="media-box-actuaries">
    <div class="media-box-actuaries_accordion">
      <div class="media-box-header-accordion">
<p>Add header content</p>
      </div>
      <div class="media-box-actuaries_accordion_content">
<p> Add content </p>
      </div>
    </div>
 </div>

你是这个意思吗? Reproduce repo

const accordion = document.getElementsByClassName ('media-box-header-accordion');

for (i = 0; i<accordion.length; i++ ){
  accordion[i].addEventListener('click', function(){
    this.parentNode.classList.toggle('active');

  })
}

您的问题是由所谓的 event bubbling.

引起的

一个解决方案是将切换 active class 的事件侦听器附加到手风琴的 header 元素(具有 class [=12= 的元素]), 像这样:

const accordions = document.getElementsByClassName("media-box-actuaries_accordion");

for (i = 0; i < accordions.length; i++) {
    const accordion = accordions[i];
    const header = accordion.querySelector(".media-box-header-accordion");

    header.addEventListener("click", function () {
        accordion.classList.toggle("active");
    });
}