选择整个div除了一些jquery的孩子

时间:2018-05-03 10:27:10

标签: jquery parent-child target

我想在点击此框时打开侧边栏,但是当我点击按钮时我不想打开侧边栏,这是我的示例代码。

<div class="kanban-level-card js-details">
  <p>Antivirus</p>
  <div class="btn-group">
    <div class="dropdown-menu">
      <button>Delete</button>
      <button>close opportunity</button>
    </div>
  </div>
</div>


$(document).on("click", ".js-details", function(e) {
  if (!$(e.target).is(".btn-group")) {
    $(".sidebar").addClass("visible");
  }
});

https://codepen.io/MahsaKiarad/pen/xjrpzx?editors=0110

1 个答案:

答案 0 :(得分:0)

只需替换

if (!$(e.target).is(".btn-group")) {

if (e.target === this) {

&#13;
&#13;
$(document).on("click", ".js-details", function(e) {
  if (e.target === this) {
    $(".sidebar").addClass("visible");
  }
});

$(".js-close").click(function() {
  $(".sidebar").removeClass("visible");
});
&#13;
.kanban-level-card {
  padding: 10px;
  margin-top: 10px;
  background-color: #eee;
  border: 3px solid green;
  color: green;
  width: 300px;
  cursor: pointer;
}


/* sidebar */

.sidebar {
  width: 400px;
  height: 100vh;
  background-color: #eee;
  box-shadow: -1px -2px 38px -3px rgba(133, 131, 133, 1);
  position: fixed;
  top: 0;
  right: -453px;
  z-index: 1000;
}

.sidebar.visible {
  right: 0px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- card -->

<div class="kanban-level-card js-details">
  <p>Antivirus</p>
  <div class="btn-group">
    <div class="dropdown-menu">
      <button>Delete</button>
      <button>close opportunity</button>
    </div>
  </div>
</div>

<!-- sidebar -->
<div class="sidebar js-sidebar">
  <button class="js-close">close</button>
</div>
&#13;
&#13;
&#13;