は、次のコードスニペットに目を通してください -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();
});
});
これはコードが後に、単一のpタグの表示を切り替えますJS h1タグをクリックすると、h1タグが表示されます。しかし、私は連続したpタグの表示を切り替える必要があります(見出し1をクリックすると1から3まで)
これを行うにはどうすればよいですか?
で導入あなたはdivの中にあなたの段落をラップする必要がありますが代わりにそれをトグルします。あなたが求めていることは可能ではありませんが、必要以上に複雑になるでしょう。 – bfavaretto