0

トグル効果を取得しようとしていますが、それを行う方法や何を探すべきかがよくわかりません。(私はJqueryをロードしました)。

に似たhtmlを仮定します

<table class="left-dates">
    <tr><td>All Dates</td></tr>
    <tr><td>01 dec 2009</td></tr>
    <tr><td>02 dec 2009</td></tr>   
    <tr><td>03 dec 2009</td></tr>   
    <tr><td>04 dec 2009</td></tr>   
</table>

<div class="box 01-dec-2009">
    foo
</div>

<div class="box 03-dec 2009">
    bar
</div>

<div class="box 04-dec-2009">
    foobar
</div>

取るべきステップ

  1. すべての div が表示されます
  2. 日付の td をクリックすると、その日のクラスがクリックされた div 以外のすべてが非表示になります
  3. 「すべての日付」をクリックすると、すべてが再び表示されます

これをきれいに達成する方法はありますか?理想的には JQuery を使用します。

4

3 に答える 3

1

私はこのようにしてみます:

var $boxes = $('div.box');

$('.left-dates td:gt(0)').click(function(e){
   var class = this.innerHTML.replace(/ /g, '-'); // Convert text to class
   $boxes.filter(':visible').not('.' + class).hide(); // All visible div.box that don't have the class we are going to show.
   $boxes.filter('.' + class).show(); // Show this class
});
$('.left-dates td:first').click(function(e){
   $boxes.show();
});

編集:に交換.click()しました.live('click')。大量の行がある場合は、イベントをそれぞれ.live()にバインドする代わりに使用する方が良いかもしれませんclick()td

また、投稿したHTMLにエラーがあります。03div の前にハイフンがありません2009

于 2009-12-04T02:04:39.373 に答える
0

jQuery を使用した実際の例を次に示します。

ラベルがクラス名と同等になるように、div クラスとtdラベルを変更して空白を削除する必要があったことに注意してください。ラベルにダッシュを入れたくない場合は、Javascript で文字列操作を行って空白を削除するか、 s に対応する div と同じクラス名を付けてから、内部テキストではなくtdクリックされたクラス名を確認します。td

<html>
<head>
    <title>jQuery hiding example</title>

    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>

    <script type='text/javascript'>
        $(document).ready(function(){
            $('td').click(function() {
                var target = $(this).text();
                if (target == 'All Dates') {
                    $('div.box').show();
                } else {
                    $('div.box').hide();
                    $('div.' + target).show();
                }
            });
        });
    </script>
</head>
<body>
    <table class="left-dates">
        <tr><td>All Dates</td></tr>
        <tr><td>01-dec-2009</td></tr>
        <tr><td>02-dec-2009</td></tr>       
        <tr><td>03-dec-2009</td></tr>       
        <tr><td>04-dec-2009</td></tr>       
    </table>

    <div class="box 01-dec-2009">
        foo
    </div>

    <div class="box 03-dec-2009">
        bar
    </div>

    <div class="box 04-dec-2009">
        foobar
    </div>
</body>
</html>
于 2009-12-04T02:15:11.927 に答える
0

<td>All Dates</td>あなたを一意に識別します。すべての日付に同じクラスまたは他の識別子を割り当てます<td>s。同じ日付のものを除くすべての .box 要素を非表示にするクリック ハンドラーを提供します。あなたの例では<td>、divの日付のクラス名と同じ日付を作成することに一貫性がありません。これは、私が行うことで必要になります。

<table class="left-dates">
    <tr><td id="alldates">All Dates</td></tr>
    <tr><td id="date">01 dec 2009</td></tr>
    <tr><td id="date">02 dec 2009</td></tr>       
    <tr><td id="date">03 dec 2009</td></tr>       
    <tr><td id="date">04 dec 2009</td></tr>       
</table>

// unhide all box elements
$("#alldates").click(function(e){ $(".box").show(); });

// For each date <td> element
$("#date").each(function(){
   // assign a click event
   $(this).click(function(e){
      // when the user clicks, get the
      // <td>'s text contents
      var myval = $(this).text();
      // and grab each element with the
      // 'box' css class
      $(".box").each(function(){
         // for each of these 'box' elements,
         // if it has a class matching the date
         // they selected, return
         if($(this).has(myval))
         {
            return;
         }
         else
         {
            // otherwise, hide itself
            $(this).hide();
         }
      });
   });
});
于 2009-12-04T02:18:55.423 に答える