Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 洗牌兄弟节点_Javascript_Jquery_Children - Fatal编程技术网

Javascript 洗牌兄弟节点

Javascript 洗牌兄弟节点,javascript,jquery,children,Javascript,Jquery,Children,我对如何洗牌元素感到有点沮丧(因为我在元素中存储了数据,仅仅更改文本是不够的)。我正在尝试洗牌兄弟元素,所以我应该使用.before和.after。这是我当前的代码 function shuffle() { var children = $("#paren").children(); randIndex = randomFromTo(0, arr.length - 1); for(i = 0; i < children.length; i++) {

我对如何洗牌元素感到有点沮丧(因为我在元素中存储了数据,仅仅更改文本是不够的)。我正在尝试洗牌兄弟元素,所以我应该使用.before和.after。这是我当前的代码

function shuffle() {
    var children = $("#paren").children();
    randIndex = randomFromTo(0, arr.length - 1);

    for(i = 0; i < children.length; i++) {
        //But obviously I can't retrieve a specific child like this
        $("#parent nth-child("+randIndex+")").before(children.i);
    }
}
function randomFromTo(from, to){
    return Math.floor(Math.random() * (to - from + 1) + from);
}
函数shuffle(){
var children=$(“#paren”).children();
randIndex=randomFromTo(0,arr.length-1);
对于(i=0;i
将子元素作为数组抓取,洗牌并再次追加。因为它们是相同的引用,所以追加实际上会移动它们

var parent = $('ul');

var children = parent.children().get().sort(function() {
    return Math.random() - 0.5;
});

parent.append(children);

构建元素数组,洗牌数组,然后重新结束元素

function shuffle(parent) {
    var container = document.getElementById(parent),
        children = container.children,
        length = children.length, i,
        tmparr = [];
    for( i=0; i<length; i++) tmparr[i] = children[i];
    tmparr.sort(function() {return 0.5-Math.random();});
    for( i=0; i<length; i++) container.appendChild(tmparr[i]);
}
函数洗牌(父级){
var container=document.getElementById(父级),
children=container.children,
长度=儿童。长度,i,
tmparr=[];

对于(i=0;iso)它有什么问题?它不起作用吗?您没有说明您的问题。。。