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

引用表的顶行

  •  0
  • Mohamad  · 技术社区  · 14 年前

    我希望下面的脚本将表行移动到表的顶部。我不知道如何引用第一行的ID,但是,由于ID是动态设置的,所以我尝试了 row.insertAfter(row.first()); 但这会使行消失而不是移动。有没有什么办法可以参考最上面一行?

        $(".top").click(function(){
            var row = $(this).parents("tr:first");
            if ($(this).is(".top")) {
                row.inserAfter(row.first());
            } else {
                return false;
            }
        });
    
    
    <tbody id="sortable">
      <tr id="row1">
        <td><a href="" class="top">Top</a></td>
      </tr>
      <tr id="row2">
        <td><a href="" class="top">Top</a></td>
      </tr>
      <tr id="row3">
        <td><a href="" class="top">Top</a></td>
      </tr>
    </tbody>
    
    4 回复  |  直到 14 年前
        1
  •  3
  •   Josh Stodola    14 年前

    This works

    $(".top").click(function() {
      var thisRow = $(this).parent().parent();
      var firstRow = thisRow.parent().find("tr:first").not(thisRow);
      thisRow.insertBefore(firstRow);
    });
    
        2
  •  1
  •   Bick    14 年前
    $(".top").click(function(){
                var tr=this.parentNode.parentNode;
        tr.parentNode.insertBefore(tr.parentNode.firstChild,tr);
        tr.parentNode.removeChild(tr);   
    }
    
        3
  •  1
  •   ArtBIT    14 年前

    $(".top").click(function(){
        var $this = $(this); // make a reference to the current element
        $.ajax({
           url: 'your/ajax/url.php',
           success: function(data) {
              // do what you want with data,
              // and then move the row on top
              // of the table, like so
              var row = $this.parents("tr");
              var first_row = row.parent().find('tr:first');
              if (row && first_row && row != first_row) {
                 row.insertBefore(first_row);
              }
           }
        });
        return false; // stay on the page
    });
    

    http://api.jquery.com/jQuery.ajax/

        4
  •  -2
  •   Diodeus - James MacFarlane    14 年前
     var row = $(this).find("tr").eq(0);