首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery accordion - onclick打开所有accordion

jquery accordion - onclick打开所有accordion
EN

Stack Overflow用户
提问于 2017-01-24 14:23:42
回答 6查看 728关注 0票数 2

我已经创建了一个自定义的jquery accordion,当我单击accordion标题时,我被代码卡住了,它打开了两个accordion,它应该在我点击的地方打开一个,以下是代码:

代码语言:javascript
复制
$(document).ready(function() {
    $(".acc-wrap > .acc-head").on("click", function() {
        if ($(this).hasClass('active')) {
            $(this).removeClass("active");
            $(this).siblings('.acc-body').slideUp(200);
            $(".acc-wrap > .acc-head").attr('class', 'acc-head opened');
        } else {
            $(".acc-wrap > .acc-head").attr('class', 'acc-head closed');
            $(this).addClass("active");
            $('.acc-body').slideUp(200);
            $(this).siblings('.acc-body').slideDown(200);
        }
    });
});
代码语言:javascript
复制
.projects-list {
  max-width: 600px;
  margin: 0 auto;
  background: #E0E0E0;
  padding: 15px 0;
  font-family: Roboto;
}
body {
  margin: 0;
  padding: 0;
}
.acc-wrap {
  position: relative;
  width: 100%;
}
.acc-wrap > .acc-head {
  float: left;
  width: 100%;
  display: block;
  background-color: #264796;
  padding: 10px;
  color: #fff;
  font-weight: 600;
  border-bottom: 1px solid #ddd;
  transition: all 0.2s linear;
  position: relative;
}
.acc-wrap > .acc-head.opened {
  position: relative;
}
.acc-wrap > .acc-head.opened:after {
  content: "\f055";
  font-family: fontAwesome;
  position: absolute;
  right: 15px;
}
.acc-wrap > .acc-head.closed {
  position: relative;
}
.acc-wrap > .acc-head.closed:after {
  content: "\f056";
  font-family: fontAwesome;
  position: absolute;
  right: 15px;
}
.acc-body {
  position: relative;
  width: 100%;
  float: left;
  background-color: #fff;
  padding: 10px;
  border-bottom: 1px solid #ddd;
  display: none;
  box-sizing: border-box;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
<div class="projects-list">
  <div class="acc-wrap">
    <div class="acc-head opened"> Vestibulum </div>
    <div class="acc-body"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna. </div>
    <div class="acc-head"> Vestibulum  2</div>
    <div class="acc-body"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna. </div>
  </div>
</div>

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2017-01-24 14:32:44

我认为你应该使用next-function而不是siblings-function:

https://api.jquery.com/next/

代码语言:javascript
复制
<style>
    .projects-list {
      max-width: 600px;
      margin: 0 auto;
      background: #E0E0E0;
      padding: 15px 0;
      font-family: Roboto;
    }
    body {
      margin: 0;
      padding: 0;
    }
    .acc-wrap {
      position: relative;
      width: 100%;
    }
    .acc-wrap > .acc-head {
      float: left;
      width: 100%;
      display: block;
      background-color: #264796;
      padding: 10px;
      color: #fff;
      font-weight: 600;
      border-bottom: 1px solid #ddd;
      transition: all 0.2s linear;
      position: relative;
    }
    .acc-wrap > .acc-head.opened {
      position: relative;
    }
    .acc-wrap > .acc-head.opened:after {
      content: "\f055";
      font-family: fontAwesome;
      position: absolute;
      right: 15px;
    }
    .acc-wrap > .acc-head.closed {
      position: relative;
    }
    .acc-wrap > .acc-head.closed:after {
      content: "\f056";
      font-family: fontAwesome;
      position: absolute;
      right: 15px;
    }
    .acc-body {
      position: relative;
      width: 100%;
      float: left;
      background-color: #fff;
      padding: 10px;
      border-bottom: 1px solid #ddd;
      display: none;
      box-sizing: border-box;
    }
</style>

`

票数 5
EN

Stack Overflow用户

发布于 2017-01-24 14:41:27

您可以尝试如下所示:

代码语言:javascript
复制
$(this)
  // Add/ Remove active and opened as both will always be together.
  .toggleClass('active opened')
  // Add closed if active not present else remove it
  .toggleClass('closed', !$this.hasClass('active'))
  // Go to next element
  .next()
  // Toggle slide on every click
  .slideToggle(200)

还要注意的是,$(".acc-wrap > .acc-head");指向所有的accordion标题。您应该始终尝试使用this并导航到必要的div。

示例

代码语言:javascript
复制
$(document).ready(function() {
  $(".acc-wrap > .acc-head").on("click", function() {
    var $this = $(this)
    $this
      .toggleClass('active opened')
      .toggleClass('closed', !$this.hasClass('active'))
      .next()
      .slideToggle(200)
  });
});
代码语言:javascript
复制
.projects-list {
  max-width: 600px;
  margin: 0 auto;
  background: #E0E0E0;
  padding: 15px 0;
  font-family: Roboto;
}
body {
  margin: 0;
  padding: 0;
}
.acc-wrap {
  position: relative;
  width: 100%;
}
.acc-wrap > .acc-head {
  float: left;
  width: 100%;
  display: block;
  background-color: #264796;
  padding: 10px;
  color: #fff;
  font-weight: 600;
  border-bottom: 1px solid #ddd;
  transition: all 0.2s linear;
  position: relative;
}
.acc-wrap > .acc-head.opened {
  position: relative;
}
.acc-wrap > .acc-head.opened:after {
  content: "\f055";
  font-family: fontAwesome;
  position: absolute;
  right: 15px;
}
.acc-wrap > .acc-head.closed {
  position: relative;
}
.acc-wrap > .acc-head.closed:after {
  content: "\f056";
  font-family: fontAwesome;
  position: absolute;
  right: 15px;
}
.acc-body {
  position: relative;
  width: 100%;
  float: left;
  background-color: #fff;
  padding: 10px;
  border-bottom: 1px solid #ddd;
  display: none;
  box-sizing: border-box;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">

<div class="projects-list">
  <div class="acc-wrap">
    <div class="acc-head">Vestibulum</div>
    <div class="acc-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.</div>
    <div class="acc-head">Vestibulum 2</div>
    <div class="acc-body">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.</div>
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2017-01-24 14:47:06

加上这一点,它比你的http://www.w3schools.com/Bootstrap/tryit.asp?filename=trybs简单得多,速度也快得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41821366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档