且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的:移动表行第一个位置,然后再

更新时间:2023-01-07 08:27:46

我要存储每个TR的位置和移动回来,如果/当需要时。

I would store each tr's position and move it back if/when needed.

我根据ShadowScripter的评论更新了code

http://jsfiddle.net/gguNM/1/

新更新
http://jsfiddle.net/gguNM/2/

$('table tr').each(function () {
  var $this  = $(this).data({position: $(this).index()}),
      $table = $this.closest('table'),
      $input = $this.find('input');

  $input.bind('click', function (e) {
    var $first = $table.find('tr:first'),
        position;

    if ($(this).is(':checked')) {
      position = $first.data('position');
      $table.find('tr input').not($(this)).removeAttr('checked');

      if (position != 0) $first.insertAfter($table.find('tr').eq(position));
      $this.prependTo($table);
    } else if ($this.data('position') != 0) {
      position = $this.data('position');
      $this.insertAfter($table.find('tr').eq(position));                
    }
  });
});​