この質問の関数を使用して、jQuery を使用してテーブルの行をシャッフルしています。私が本当に望んでいるのは、次のように、最初の列のみをシャッフルし、残りはそのままにしておくことです。
a b c d
e f g h
i j
これに:
e b c d
i f g h
a j
Rob Wの答えの機能は次のとおりです。
(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)
しかし、これは私が望んでいたこととは反対のことをします。つまり、最初の列を除くすべての行をシャッフルします。最初の列のみをシャッフルするようにこれを変更するにはどうすればよいですか?