アコーディオンメニューを作成したのですが、うまくいきません。
ご教示いただけないでしょうか。
よろしくお願いいたします。
1.最初の一つが開いた状態
2.他のメニューを開くと、開いていたメニューが閉じる
3.クリックした時に画像が変わる(よくある+-ボタンのことです)
「3」はopenというクラスを付けて何とかしようと思ったのですが、付け方がおかしいため、
うまく動かない状態です。
「2」はこのコードでうまく動いているようです。
<div class="box"> <dl> <dt class="open">テスト</dt> <dd>テスト</dd> </dl> <dl> <dt>テスト</dt> <dd>テスト</dd> </dl> <dl> <dt>テスト</dt> <dd>テスト</dd> </dl> </div>
$(function(){ $(".box dl dd").hide(); $(".box dl dt").click(function(){ $(this).addClass("open"); $(".box dl dd").slideUp(); if($("+dd",this).css("display")=="none"){ $("+dd",this).slideDown(); $(this).removeClass("open"); } }); });
.box dl { position: relative; width: 300px; margin: auto; } .box dl dt { height: 30px; margin: auto; text-indent: 100%; white-space: nowrap; overflow: hidden; } .box dl:nth-of-type(1) dt { background: url(https://placehold.jp/300x30.png) 0 0 no-repeat; } .box dl:nth-of-type(1) dt.open { background: url(https://placehold.jp/3d4070/ffffff/300x30.png) 0 0 no-repeat; } .box dl:nth-of-type(2) dt { background: url(https://placehold.jp/300x30.png) 0 0 no-repeat; } .box dl:nth-of-type(2) dt.open { background: url(https://placehold.jp/3d4070/ffffff/300x30.png) 0 0 no-repeat; } .box dl:nth-of-type(3) dt { background: url(https://placehold.jp/300x30.png) 0 0 no-repeat; } .box dl:nth-of-type(3) dt.open { background: url(https://placehold.jp/3d4070/ffffff/300x30.png) 0 0 no-repeat; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/08 04:51