【问题标题】:Trigger inner class while activating container class as 'this'在将容器类激活为“this”时触发内部类
【发布时间】:2017-06-15 10:30:42
【问题描述】:

在我当前的示例中,您会看到 '.collapse' 容器,当有人在 div 本身上单击 anywhere 时会触发该容器。有很多 '.collapse' div,但由于 JS 中的 'this',每个 div 都是单独触发的。问题是我不希望整个容器成为触发器。

如何在 JS 中替换 '.collapse' 并使用 h2:after 但仍将 .collapse 触发为 'this'。

让我知道这是否令人困惑,以便我可以编辑我的问题以使其更易于理解。

HTML

<div class="collapse">
<h2>sometext
::after
</h2>
</div>

JS

$('.collapse').on('click',function(e){
  e.preventDefault();
  $(this).toggleClass('active');
});

http://codepen.io/mariomez/pen/jyaWax

【问题讨论】:

    标签: javascript jquery css class this


    【解决方案1】:

    我在您的代码中没有看到任何h6,但您可以使用h2 并使用$(this) 和parent() 来选择它的父级。我希望这是您正在寻找的:

    $('h2').on('click', function(e) {
      $(this).parent().toggleClass('active');
    });
    @import url(http://fonts.googleapis.com/css?family=Roboto);
     html {
      height: 100%
    }
    body {
      font-family: 'Roboto', sans-serif;
      font-size: 15px;
      -webkit-user-select: none;
      -khtml-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none; background:url(https://unsplash.imgix.net/40/OTwgwURiQN6DLk8zIr8E_DSC00953.jpg?q=75&w=1080&h=1080&fit=max&fm=jpg&auto=format&s=408af6f15369c9d232097a79ff997fa7) center no-repeat;
      background-size: cover
    }
    h1 {
      color: #fff;
      text-align: center
    }
    .wrap {
      width: 80%;
      margin: 0 auto;
    }
    .collapse {
      background-color: rgba(255, 255, 255, 0);
      border-bottom: 1px solid #eee;
      cursor: pointer;
      color: #fff;
      padding: 10px;
      margin: 0px;
      max-height: 40px;
      overflow: hidden;
      transition: all 0.4s;
    }
    .collapse * {
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
    }
    .collapse.active {
      background-color: rgba(255, 255, 255, 0.9);
      box-shadow: 0 8px 17px 0 rgba(0, 0, 0, 0.2);
      z-index: 200;
      color: #444;
      max-height: 3000px;
      padding: 10px 20px;
      margin: 10px -10px;
      transition: all 0.2s, max-height 4.8s;
    }
    .collapse h2 {
      font-size: 18px;
      line-height: 20px;
      position: relative
    }
    .transparent {
      background-color: rgba(255, 255, 255, 0) !important;
      color: #fff !important;
      box-shadow: none !important;
      margin: 0px !important;
      padding: 10px !important
    }
    .collapse h2::after {
      content: "+";
      text-align: center;
      position: absolute;
      width: 15px;
      height: 15px;
      border: 1px solid #ccc;
      border-radius: 50%;
      font-size: 12px;
      line-height: 15px;
      opacity: 0.5;
      right: 0;
      top: 0;
    }
    .collapse:hover h2::after {
      opacity: 1
    }
    .collapse.active h2::after {
      content: "-";
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="wrap">
      <h1>Collapsible drawers</h1>
    
      <div class="collapse transparent">
        <h2>I am transparent</h2>
        The brain is like a muscle. When it is in use we feel very good. Understanding is joyous.
        <b>Carl Sagan</b> 
        <p>This one keeps the transparency</p>
      </div>
    </div>

    【讨论】:

    • 我编辑了我的问题。我不敢相信我忘记了“父母”。谢谢你,我的朋友。
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多