简体   繁体   English

仅扩展父级内的子级

[英]Expand Only the Child Div Within Parent

I'm trying to only show a child div when the parent div's text is shown, instead of all the other divs with the same name. 我正在尝试仅在显示父div的文本时显示子div,而不是显示所有其他具有相同名称的div。 Imagine four div's with classes of "lead" and within those leads, there is a normaldiv child. 想象一下四个div与“领导”类,在这些领导中,有一个normaldiv孩子。 Rather than open all the childs in other lead divs, I only want the one child within each lead. 我不想打开其他领导div中的所有孩子,而只想要每个领导者中的一个孩子。

Example HTML: 示例HTML:

<div class="lead"><div class="normaldiv">1</div></div>
<div class="lead"><div class="normaldiv">2</div></div>
<div class="lead"><div class="normaldiv">3</div></div>
<div class="lead"><div class="normaldiv">4</div></div>

jQuery - this expands all normal divs jQuery - 这扩展了所有正常的div

$(".lead").click(function() {
  $(".normaldiv").slideToggle("slow");
});

Tested: 测试:

$(this).parent().children(".normaldiv").slideToggle("slow");

The tested one I thought would grab all the children within the parent div and only expand those on a click. 我认为经过测试的那个将抓住父div中的所有子节点,并且只在点击时展开它们。 However, nothing shows up on a click event. 但是,单击事件不会显示任何内容。

Use $(this).find(".normaldiv") inside your click listener to select only the normaldiv inside the lead element - see demo below: 点击监听器中使用$(this).find(".normaldiv") 选择lead元素中的normaldiv - 请参阅下面的演示:

 $(".lead").click(function() { $(this).find(".normaldiv").slideToggle("slow"); }); 
 <div class="lead"><div class="normaldiv">1</div></div> <div class="lead"><div class="normaldiv">2</div></div> <div class="lead"><div class="normaldiv">3</div></div> <div class="lead"><div class="normaldiv">4</div></div> 

grab only the children not the parent children 只抓住孩子而不是父母的孩子

 $(".lead").click(function() { // $(".normaldiv").slideToggle("slow"); $(this).children(".normaldiv").slideToggle("slow"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="lead"><div class="normaldiv">1</div></div> <div class="lead"><div class="normaldiv">2</div></div> <div class="lead"><div class="normaldiv">3</div></div> <div class="lead"><div class="normaldiv">4</div></div> 

You need to select the children of the .lead elements that you are selecting. 你需要选择的儿童 .lead您选择的元素。

What you are doing currently in the second example is selecting the parent of the .lead element (some container element), and then trying to select .normaldiv out of its children (which are only .lead elements). 当前在第二个示例中您正在做的是选择.lead元素的父元素(某个容器元素),然后尝试从其子元素中选择.normaldiv (它们只是.lead元素)。

 $(".lead").click(function() { $(this).children(".normaldiv").slideToggle("slow"); }); 
 .lead { height: 20px; width: 20px; background-color: grey; margin: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="lead"><div class="normaldiv">1</div></div> <div class="lead"><div class="normaldiv">2</div></div> <div class="lead"><div class="normaldiv">3</div></div> <div class="lead"><div class="normaldiv">4</div></div> 

Note: CSS added for better visibility. 注意:添加了CSS以提高可见性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM