請通過下面的代碼片段看起來 -如何使用jQuery獲取連續的元素?
HTML
<div class="aclass">
<h1>This is heading one</h1>
<p>This is paragraph one, this will be hidden automatically</p>
<p>This is paragraph two, this will be hidden automatically</p>
<p>This is paragraph three, this will be hidden automatically</p>
<h1>This is heading two</h1>
<p>This is paragraph four, this will be hidden automatically</p>
<p>This is paragraph five, this will be hidden automatically</p>
</div>
CSS
.aclass p {display:none;}
JS
$(document).ready(function(){
$('.aclass h1').click(function(){
$(this).next('p').toggle();
});
});
這個js代碼切換單P標籤後顯示點擊h1標籤時的h1標籤。但我需要切換顯示連續的p標籤(點擊標題之一時爲1到3)
什麼應該是jQuery代碼這樣做?
你應該包裝在一個div段落,然後切換。並不是說你所要求的是不可能的,但它會比所需要的更加複雜。 – bfavaretto 2012-02-24 21:45:13