有没有办法将点击事件附加到多个元素上,然后让除了被点击的元素及其子元素之外的所有子元素都运行一个函数?
我的解释可能比较糟糕,但我想要实现的效果就是这样:
$(".block_header").click(function(){
$(".block_header span:not(this span)").toggleClass("collapse expand");
$(".container:not(this+div.container)").slideToggle();
});
下面是一个HTML示例:
<h3 id="sender_header" class="block_header"><span>Sender</span></h3>
<div id="sender_container" class="container">
<p>Show or hide this, based on which H3 is clicked.</p>
</div>
<h3 id="receiver_header" class="block_header"><span>Receiver</span></h3>
<div id="receiver_container" class="container">
<p>Show or hide this, based on which H3 is clicked.</p>
</div>
<h3 id="delivery_header" class="block_header"><span>Delivery</span></h3>
<div id="delivery_container" class="container">
<p>Show or hide this, based on which H3 is clicked.</p>
</div>
<h3 id="items_header" class="block_header"><span>Items</span></h3>
<div id="items_container" class="container">
<p>Show or hide this, based on which H3 is clicked.</p>
</div>
.... etc, etc (many more of the same)
希望这不会太复杂。它会为我节省很多代码。