代码之家  ›  专栏  ›  技术社区  ›  fire

jQuery切换tbody

  •  1
  • fire  · 技术社区  · 14 年前

    <th class="folder">

        $('table').each(function(){
            $('th.folder', this).bind('click', function(){
                $(this).closest('table').children('tbody').toggle();
            });
        });
    

    下面是一些典型的HTML:

    <table>
      <thead>
        <tr>
          <th>Title</th>
          <th class="type">Type</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <table>
              <thead>
                <tr>
                  <th class="folder" colspan="2">Deal flow</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>item 1</td>
                  <td>image</td>
                </tr>
                <tr>
                  <td>item 2</td>
                  <td>image</td>
                </tr>
              </tbody>
            </table>
            <table>
              <thead>
                <tr>
                  <th class="folder" colspan="2">Rejected deals</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>item 3</td>
                  <td>image</td>
                </tr>
                <tr>
                  <td>item 4</td>
                  <td>image</td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    

    好像不起作用,我不明白为什么!有什么想法吗?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Sarfraz    14 年前

    See Working Example


    请改为:

      $('.folder').bind('click', function(){
         $(this).closest('table').children('tbody').toggle();
      });
    
        2
  •  0
  •   tbleckert    14 年前

    另外,我将使用live()而不是bind(),因为live()更快。