jquery.children 仅适用于第一个 child

jquery.children works only for the first child

我想 .slideToggle() 在单击每个 parent 时滑动 class,因此我使用 .children() 访问 it.but,如您在该片段仅适用于第一个 div!!

我也用过 .find() 而不是 .children() 但是没用。:(
如果有人能告诉我我的问题,我将不胜感激。

$(document).ready(function() {
 $("#open").click(function() {
       $(this).children('#slide').slideToggle();
    });
});
*{
 margin:0;
 padding:0;
}
body{
 background-color:#ecf0f5;
 overflow-x: hidden;
}
.divider {
  height: 1px;
  width:100%;
  display:block; /* for use on default inline elements like span */
  margin: 9px 0;
  overflow: hidden;
  background-color: #9b9b9b;
  margin-top:2px !important;
}
.pointer{
 cursor:pointer;
}.slide{
 background:#e2e2e2;
 padding:20px;
 display:none;
 margin:10px;
}.full{
  width:100%;
}.width{
  width:90%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="full pointer" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;" id="open">
<input type="checkbox" style="-webkit-transform:scale(1.15);" /><br>
<div class="slide width" id="slide">
txt
</div>
</div>                            
<div class="divider" style="margin:0;"></div>
<div class="full pointer" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;" id="open">
<input type="checkbox" style="-webkit-transform:scale(1.15);" /><br>
<div class="slide width" id="slide">
txt
</div>
</div>                            
<div class="divider" style="margin:0;"></div>
<div class="full pointer" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;" id="open">
<input type="checkbox" style="-webkit-transform:scale(1.15);" /><br>
<div class="slide width" id="slide">
txt 
</div>
</div>                            
<div class="divider" style="margin:0;"></div>

多次使用相同的 id 是无效的。对 js 部分使用 classes,它将正常工作。

$(document).ready(function() {
  $(".open").click(function() {
    $(this).children('.slide').slideToggle();
  });
});

检查一下,让我知道您的反馈。谢谢!

$(document).ready(function() {
  $(".open").click(function() {
    $(this).children('.slide').slideToggle();
  });
});
* {
  margin: 0;
  padding: 0;
}
body {
  background-color: #ecf0f5;
  font-family: IRANSans;
  overflow-x: hidden;
}
.divider {
  height: 1px;
  width: 100%;
  display: block;
  /* for use on default inline elements like span */
  margin: 9px 0;
  overflow: hidden;
  background-color: #9b9b9b;
  margin-top: 2px !important;
}
.pointer {
  cursor: pointer;
}
.slide {
  background: #e2e2e2;
  padding: 20px;
  display: none;
  margin: 10px;
}
.full {
  width: 100%;
}
.width {
  width: 90%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="full pointer open" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;">
  <input type="checkbox" style="-webkit-transform:scale(1.15);" />
  <br>
  <div class="slide width" id="slide">
    txt
  </div>
</div>
<div class="divider" style="margin:0;"></div>
<div class="full pointer open" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;">
  <input type="checkbox" style="-webkit-transform:scale(1.15);" />
  <br>
  <div class="slide width" id="slide">
    txt
  </div>
</div>
<div class="divider" style="margin:0;"></div>
<div class="full pointer open" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;">
  <input type="checkbox" style="-webkit-transform:scale(1.15);" />
  <br>
  <div class="slide width" id="slide">
    txt
  </div>
</div>
<div class="divider" style="margin:0;"></div>

document 中元素的

id 应该是唯一的。在 #open#slide 处使用 class 代替 id,在 html 和选择器处重复 id,例如 .open.slidejavascript

$(document).ready(function() {
  $(".open").click(function() {
    $(this).children(".slide").slideToggle();
  });
});
* {
  margin: 0;
  padding: 0;
}
body {
  background-color: #ecf0f5;
  font-family: IRANSans;
  overflow-x: hidden;
}
.divider {
  height: 1px;
  width: 100%;
  display: block;
  /* for use on default inline elements like span */
  margin: 9px 0;
  overflow: hidden;
  background-color: #9b9b9b;
  margin-top: 2px !important;
}
.pointer {
  cursor: pointer;
}
.slide {
  background: #e2e2e2;
  padding: 20px;
  display: none;
  margin: 10px;
}
.full {
  width: 100%;
}
.width {
  width: 90%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="full pointer open" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;">
  <input type="checkbox" style="-webkit-transform:scale(1.15);" />
  <br>
  <div class="slide width" class="slide">
    txt
  </div>
</div>
<div class="divider" style="margin:0;"></div>
<div class="full pointer open" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;">
  <input type="checkbox" style="-webkit-transform:scale(1.15);" />
  <br>
  <div class="slide width">
    txt
  </div>
</div>
<div class="divider" style="margin:0;"></div>
<div class="full pointer open" style="background:#f4f4f4;display:inline-block;padding:7px 14px;margin:0;">
  <input type="checkbox" style="-webkit-transform:scale(1.15);" />
  <br>
  <div class="slide width">
    txt
  </div>
</div>
<div class="divider" style="margin:0;"></div>