[英]jQuery selector parents and siblings
I have a div
structure that I can't change.我有一个无法更改的
div
结构。 In an outer-container
are two inner-container
s.在一个
outer-container
中有两个inner-container
。 I want to toggle the inner-container2
.我想切换
inner-container2
。 The button is in the first inner-container
.该按钮位于第一个
inner-container
。
This structure repeats again and again.这种结构一次又一次地重复。 So I can't select the classname (
inner-container2
).所以我不能 select 类名(
inner-container2
)。 This classname appears often.这个类名经常出现。
I must select something like: go one level up from the button and take the next div
我必须 select 之类的东西: go 从按钮上一层并取下一个
div
<div class="outer-container">
<div class="inner-container1">
<div class="button">
</div>
</div>
<div class="inner-container2">
</div>
</div>
This group comes again and again.这个群体一次又一次地出现。
I want to have a script that starts with a click on button and opens only the inner-container2
in the same group.我想要一个以单击按钮开始并仅打开同一组中的
inner-container2
的脚本。
$(document).on('click', '.button', function(){
$('?????').toggle('slow');
});
$(document).on('click', '.button', function() { $(this).parent().next().toggle('slow'); });
.inner-container2{ background-color:gray; height: 50px; width: 50px }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="outer-container"> <div class="inner-container1"> <div class="button"> <button>BUTTON1</button> </div> </div> <div class="inner-container2"></div> </div>
I have made an example using your code just added css to define the functionality.我已经使用您刚刚添加的代码 css 来定义功能做了一个示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.