7

私はjavaScriptとjQueryにかなり慣れていないので、これが簡単な修正になることを願っています。いくつかのカテゴリにグループ化できるデータを含むテーブルを表示する必要があり、特定の各カテゴリのすべての観測値を非表示/表示するslideToggleを実装したいと思います。

以下のコードは、(理想的には)4列9行のテーブルを表示し、3行のすべてのグループの前に緑色の「セクションi」行を表示する必要があります。各セクションヘッダーを、その下のすべての行を展開または折りたたむslideToggleとして機能させたいと思います。現在、崩壊しているものはありません。何かご意見は?

<head>
  <style type="text/css">
    td{padding:5px;}
  </style>

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript"> 
      $(document).ready(function(){
      $(".flip").click(function(){
          $(this).next(".section").slideToggle();
      });
  });
  </script>

</head>

<body>
    <p>
        <table id="main_table">
        <thead>
            <tr class="firstline">
                <th>Column1</th>
                <th>Column2</th>
                <th>Column3</th>
                <th>Column4</th>
            </tr>
        </thead>
        <tbody>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 1 </td> 
            </tr>
            <div class="section">
            <tr>
                <td>item 111</td>
                <td>item 112</td>
                <td>item 113</td>
                <td>item 114</td>
            </tr>
            <tr>
                <td>item 121</td>
                <td>item 122</td>
                <td>item 123</td>
                <td>item 124</td>
            </tr>
            <tr>
                <td>item 131</td>
                <td>item 132</td>
                <td>item 133</td>
                <td>item 134</td>
            </tr>
            </div>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 2 </td> 
            </tr>
            <div class="section">
            <tr>
                <td>item 211</td>
                <td>item 212</td>
                <td>item 213</td>
                <td>item 214</td>
            </tr>
            <tr>
                <td>item 221</td>
                <td>item 222</td>
                <td>item 223</td>
                <td>item 224</td>
            </tr>
            <tr>
                <td>item 231</td>
                <td>item 232</td>
                <td>item 233</td>
                <td>item 234</td>
            </tr>
            </div>
            <tr style="background-color:green; color:white"> 
                <td  colspan="4" class="flip"> Section 3 </td> 
            </tr>
            <div class="section">
            <tr>
                <td>item 311</td>
                <td>item 312</td>
                <td>item 313</td>
                <td>item 314</td>
            </tr>
            <tr>
                <td>item 321</td>
                <td>item 322</td>
                <td>item 323</td>
                <td>item 324</td>
            </tr>
            <tr>
                <td>item 331</td>
                <td>item 332</td>
                <td>item 333</td>
                <td>item 334</td>
            </tr>
            </div>
        </tbody>
        </table>
    </p>
</body>
4

1 に答える 1

5

<div>sをそのようなものに混ぜることはできません。代わりに<table>、追加<tbody>の要素を使用してください。コールバックでthisは、は<td>兄弟を.next持たない要素であるため、何も役に立ちません。.section興味のあるものと同じ深さの何かに到達するまで戻って、.nextそこから電話をかけたいと思います。

HTMLは次のようになります。

<table id="main_table">
    <thead>
        <tr class="firstline">
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
            <th>Column4</th>
        </tr>
    </thead>
    <tbody>
        <tr style="background-color:green; color:white">
            <td  colspan="4" class="flip"> Section 1 </td>
        </tr>
    </tbody>
    <tbody class="section">
        <tr>
            <td>item 111</td>
            <td>item 112</td>
            <td>item 113</td>
            <td>item 114</td>
        </tr>
        <!-- ... -->

そして、次のようなクリックハンドラー:

$('.flip').click(function() {
    $(this)
        .closest('tbody')
        .next('.section')
        .toggle('fast');
});

呼び出しは、.closestが見つかるまで先祖をバックアップし、<tbody>それから呼び出します.next

更新されたjsfiddle:http: //jsfiddle.net/ambiguous/Udxyb/4/

于 2011-02-25T03:39:27.597 に答える