【发布时间】:2012-09-04 06:40:43
【问题描述】:
我正在使用来自this question 的函数来使用jQuery 对表的行进行洗牌。我真正想要的是只洗牌第一列,其余部分保持不变,如下所示:
a b c d
e f g h
i j
到这里:
e b c d
i f g h
a j
Rob W's answer中的函数是:
(function($){ //Shuffle all rows, while keeping the first column //Requires: Shuffle $.fn.shuffleRows = function(){ return this.each(function(){ var main = $(/table/i.test(this.tagName) ? this.tBodies[0] : this); var firstElem = [], counter=0; main.children().each(function(){ firstElem.push(this.firstChild); }); main.shuffle(); main.children().each(function(){ this.insertBefore(firstElem[counter++], this.firstChild); }); }); } /* Shuffle is required */ $.fn.shuffle = function() { return this.each(function(){ var items = $(this).children(); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); return arr; } })(jQuery)
但这与我想要的相反——它打乱了所有行但第一列。我怎样才能改变它,让它只洗牌第一列?
【问题讨论】: