【问题标题】:Show only sibling div with specific class仅显示具有特定类的兄弟 div
【发布时间】:2015-10-01 20:10:35
【问题描述】:

我用一个例子做了一个codepen。

http://codepen.io/anon/pen/vNgvwV

当您单击按钮并且它变为“绿色”时 - 选中,应该显示同级“time_container”。

$(".button_cell").click(function() {
  var self = this, // Cache
    div1 = $('.check_button', self), // Cache
    div2 = $('.checked_button', self), // Cache
    div3 = $('.red_check', self), // Cache
    divtemp = $('.time_container', self); // Cache

  if (div1.is(':visible')) {
    div1.hide();
    $(div2, divtemp).show();
  } else if (div2.is(':visible')) { // Use else if
    div3.show();
    $(div2, divtemp).hide();
  } else if (div3.is(':visible')) { // Use else if
    div3.hide();
    div1.show(); // Use multiple selectors
  }
});
body {
  background: #fafafa;
  height: 100vh;
  width: 100vw;
  color: #282828;
  margin: 0;
}
.button_cell {
  cursor: pointer;
  display: block;
  height: 100px;
  width: 100px;
  -webkit-user-select: none;
  /* Chrome/Safari */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* IE10+ */
  margin: 10px;
}
.check_button {
  height: 100px;
  width: 100px;
  display: block;
  background: gray;
}
.checked_button {
  height: 100px;
  width: 100px;
  display: none;
  background: green;
}
.red_check {
  height: 100px;
  width: 100px;
  display: none;
  background: red;
}
.time_container {
  display: none;
  height: 100px;
  width: 100px;
  background: blue;
  color: white;
  margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="button_cell">
    <div class="check_button">Empty</div>
    <div class="checked_button">Checked</div>
    <div class="red_check">Red Checked</div>
  </div>
  <div class="time_container">Time Container</div>
</div>

<div class="row">
  <div class="time_container">This one should not be shown</div>

我无法让它工作。 有什么想法吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您已经将 jQuery 对象存储在 divtemp 中,因此 $(div2, divtemp) 将不起作用,因为 div2 ,divtemp 不是有效的选择器,它们是对象。

    $(".button_cell").click(function() {
      var self = this, // Cache
        div1 = $('.check_button', self), // Cache
        div2 = $('.checked_button', self), // Cache
        div3 = $('.red_check', self), // Cache
        divtemp = $('.time_container'); // Cache
    
      if (div1.is(':visible')) {
        div1.hide();
       div2.show();divtemp.show();
      } else if (div2.is(':visible')) { // Use else if
        div3.show();
        $(div2, divtemp).hide();
      } else if (div3.is(':visible')) { // Use else if
        div3.hide();
        div1.show(); // Use multiple selectors
      }
    });
    body {
      background: #fafafa;
      height: 100vh;
      width: 100vw;
      color: #282828;
      margin: 0;
    }
    .button_cell {
      cursor: pointer;
      display: block;
      height: 100px;
      width: 100px;
      -webkit-user-select: none;
      /* Chrome/Safari */
      -moz-user-select: none;
      /* Firefox */
      -ms-user-select: none;
      /* IE10+ */
      margin: 10px;
    }
    .check_button {
      height: 100px;
      width: 100px;
      display: block;
      background: gray;
    }
    .checked_button {
      height: 100px;
      width: 100px;
      display: none;
      background: green;
    }
    .red_check {
      height: 100px;
      width: 100px;
      display: none;
      background: red;
    }
    .time_container {
      display: none;
      height: 100px;
      width: 100px;
      background: blue;
      color: white;
      margin: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="row">
      <div class="button_cell">
        <div class="check_button">Empty</div>
        <div class="checked_button">Checked</div>
        <div class="red_check">Red Checked</div>
      </div>
      <div class="time_container">Time Container</div>
    </div>
    
    <div class="row">
      <div class="time_container">This one should not be shown</div>

    【讨论】:

      【解决方案2】:

      请通过更新后的 codepen 链接 - CodePen

      还请注意,您之前引用 $(div2, divtemp).show(); 的代码仅引用第一个对象而不是第二个对象“divtemp”

      【讨论】:

      • 您好,谢谢,我可以看到它在您的 sn-p 中按预期工作,但我无法让它在我的网站上工作。另外,我不明白哪一行代码只显示同级容器,而不显示另一个?
      • 啊,我明白了。你没有关闭 button_cell div,所以你把 time_container 变成了一个孩子?
      • 在按钮单击下的代码中定义的“var self = this”行,您使用此按钮引用同级容器,因此它不会影响其他容器
      • 也可以尝试删除'divtemp = $('.time_container', self);中的self '你会明白的
      • 我尝试在 time_container 之后删除“,self”,然后它可以工作,但它显示所有带有 time_container 的 div。如果我重新添加 self ,则它不起作用,因为在我的代码示例中,time_container div 是一个 SIBLING 而不是像上面的代码示例中的一个 CHILD。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多