2017-02-13 3 views
1

以下、 'plus'クラスをクリックすると、すべての非表示行が表示されます。別のクラスがクリックされたときにクラスを削除/追加する方法は?

デフォルトでは非表示になっています。

特定の年がクリックされたときにどうすればその年の月が表示されますか。

以下のコードをクリックすると、すべて表示されます。

<cfoutput query="getdates" group="year"> 
    <thead> 
    <tr> 
     <th> <span class="plus">+</span> #year#</th> 
    </tr> 
    </thead> 
    <cfif dtToday gte combineDates> 
    <tbody class="closeAction"> 
     <cfoutput> 
     <tr> 
      <td><a href=""> #month# </a></td> 
     </tr> 
     </cfoutput> 
    </tbody> 
    </cfif> 
</cfoutput> 

<style> 
    .closeAction { 
    display: none; 
    } 
</style> 

<script> 
    $(".plus").click(function() { 
    //$('.plus',this).html('-'); 
    $(".closeAction").removeClass("closeAction"); 
    }) 
</script> 

これは、クリックする前にどのように見えるかです:

enter image description here

+0

複数の日付を示す最終的なHTMLをもう少し分かち合うことができたら、自分の答えを私が提供したものよりも良くなるように整理することができます。 – AtheistP3ace

答えて

1

使用closest()next()後(提供クラス名で次の要素を検索し、親に行きます)。

closeActionにADITIONでcloseクラスを追加することも、なぜノートそして/終了日を開くためにtoggleClassを使用します。ここ

が働いfiddle

そしてあるスニペット

$(".plus").click(function() { 
 
    $(this).closest('thead').next('.closeAction').toggleClass('closed'); 
 
})
.closed { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <thead> 
 
    <tr > 
 
     <th> <span class="plus">+</span> 2015</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody class="closeAction closed"> 
 
    <tr> 
 
     <td><a href="#"> Jan </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Feb </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Mar </a></td> 
 
    </tr> 
 
    <thead> 
 
    <tr > 
 
     <th> <span class="plus">+</span> 2016</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody class="closeAction closed"> 
 
    <tr> 
 
     <td><a href="#"> #month# </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Jan </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Feb </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Mar </a></td> 
 
    </tr> 
 
    <thead> 
 
    <tr > 
 
     <th> <span class="plus">+</span> 2017</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody class="closeAction closed"> 
 
    <tr> 
 
     <td><a href="#"> Jan </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Feb </a></td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="#"> Mar </a></td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+0

ありがとう、この作品は今私はマイナス記号を追加してそれを把握する必要があります:) –

+0

グレート:)私は嬉しいです。 ( "プラス")。マイナス記号 '$のためのanatp_123 @ –

+1

クリック(関数(){ \t \tするvarテキスト= '+'; 場合($(この)は.text()== '+') toggleClass( 'closed'); }) 'Fiddle:https( ')'と入力してください。{ テキスト= ' - '; } $(this) ://jsfiddle.net/5yowntua/1/ – AtheistP3ace

0

何をやっているクラスcloseActionを持つすべての要素を考慮しています。あなたは次のものを誘発する必要があります。だから、あなたはnext()

<script> 
$(".plus").click(function() { 
//$('.plus',this).html('-'); 
    $(this).next(".closeAction").removeClass("closeAction"); 
}) 
</script> 
1

$(".closeAction")を使用することができますなぜ彼らすべてが開いているDOMに、このクラスですべての要素を見つけることです。そのクラスに関連付けられている要素を見つける必要があります。 thisを使用すると、DOMを上に移動して、そのクラスの要素をイベントのコンテキストから検索できます。 thisはクリックイベントのコンテキストで、イベントが添付された要素はプラスです。

$(this).closest('thead').next().find('.closeAction').removeClass("closeAction"); 

これは、最も近いthead要素まで移動次の要素を取得し、その内部にcloseActionクラスを持つすべての要素を検索します。あなたは、このコードは、最も可能性の高い改善される可能性がHTMLのより多くのではなく、我々は唯一それがどのように見えるかを知るために、そのハードプラスとcloseActionの単一例で作業しているので、例を示すことができた場合

0

.plus.closeAction要素は、あなたが最も近いcfoutput要素までトラバースし、そこから、.closeAction要素を見つけることができるので、同じcfoutput要素を共有しているようです。

$(".plus").click(function() { 
    $(this).closest("cfoutput").find('.closeAction').removeClass("closeAction"); 
}) 
+0

私がやりたかったことですが、それらの属性 'query =" getdates "group =" year "'は、すべての年が 'cfoutput'の下でグループ化されているように思えます。私の最初の気持ちから、最高の要素を見つけて見つけるだけだったので、私が持っていた考えを分かち合いました。しかし、すべての年が 'cfoutput'の下でグループ化されていれば、' closeAction'sはすべて – AtheistP3ace

+0

となります。私は、正しいソリューションを得るために、彼の事例でより多くのコードを提供する必要があると思います。 – Mikey

+0

絶対に同意してください!私は彼らに尋ねた。 – AtheistP3ace

関連する問題