我有一个简单的脚本切换父元素中的元素,但是,我还需要分别切换父元素之外的任何其他元素。不完全确定如何做到这一点。这就是我到目前为止所做的:
$('.card-toggle').on('click', function() {
$(this).parent().siblings('.card-img, .card-text').toggle(250);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card">
<div class="card-img">
<img src="img.jpg" data-location="local">
</div>
<div class="button-container">
<button class="card-toggle">Domy</button>
</div>
<p class="card-text">
<span style="font-size:0.9em">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</span>
</p>
</div>
&#13;
答案 0 :(得分:1)
您可以使用类card-toggle
使用类选择器,使用.not()
排除当前元素,并对它们执行所需的操作。
$('.card-toggle').not(this).parent().siblings('.card-img, .card-text').hide(250);
$('.card-toggle').on('click', function() {
$(this).parent().siblings('.card-img, .card-text').toggle(250);
$('.card-toggle').not(this).parent().siblings('.card-img, .card-text').hide(250);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card">
<div class="card-img">
<img src="img.jpg" data-location="local">
</div>
<div class="button-container">
<button class="card-toggle">Domy</button>
</div>
<p class="card-text">
<span style="font-size:0.9em">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</span>
</p>
</div>
<div class="card">
<div class="card-img">
<img src="img.jpg" data-location="local">
</div>
<div class="button-container">
<button class="card-toggle">Domy</button>
</div>
<p class="card-text">
<span style="font-size:0.9em">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</span>
</p>
</div>
&#13;
或者您也可以使用
var siblings = $(this).parent().siblings('.card-img, .card-text');
siblings.toggle(250);
$('.card-img, .card-text').not(siblings).hide(250);
$('.card-toggle').on('click', function() {
var siblings = $(this).parent().siblings('.card-img, .card-text');
siblings.toggle(250);
$('.card-img, .card-text').not(siblings).hide(250);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card">
<div class="card-img">
<img src="img.jpg" data-location="local">
</div>
<div class="button-container">
<button class="card-toggle">Domy</button>
</div>
<p class="card-text">
<span style="font-size:0.9em">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</span>
</p>
</div>
<div class="card">
<div class="card-img">
<img src="img.jpg" data-location="local">
</div>
<div class="button-container">
<button class="card-toggle">Domy</button>
</div>
<p class="card-text">
<span style="font-size:0.9em">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate</span>
</p>
</div>
&#13;