我正在使用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)
但这与我想要的相反 - 它将所有行洗牌但第一栏。我怎样才能改变这个让它洗牌只有第一列?
谢谢!顺便说一句,我不明白[]做什么? –
回答更新了为什么'[] .sort.call($ copies,...)'对'$ copies' jQuery对象进行排序的简要解释。 (另外,如果你想得到的答案是你所链接的代码的修改版本,但是这段代码看起来太长而且很复杂,因为我认为它应该是一个相当简单的操作,所以我忽略了它。) – nnnnnn