Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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-Codepen尝试使用数组更改html内容的顺序我的函数正在运行,但效果不太好';我不明白为什么_Javascript_Html_Arrays - Fatal编程技术网

Javascript-Codepen尝试使用数组更改html内容的顺序我的函数正在运行,但效果不太好';我不明白为什么

Javascript-Codepen尝试使用数组更改html内容的顺序我的函数正在运行,但效果不太好';我不明白为什么,javascript,html,arrays,Javascript,Html,Arrays,嗨!如果有人能帮我,我不知道是我的代码逻辑错了还是我犯了一些错误,所以请看一下 我创建了一个名为“多米诺骨牌”的数组,包含13个条目 我用我的“lien”函数将每个dominos条目与html的每个div class=domino链接起来 我用“洗牌”功能洗牌多米诺骨牌阵列 我在文档中发回我的div的新订单 ` const reload=(数组)=>{ 设len=array.length-1; 对于(var i=0;i首先,您的所有随机函数都会生成一些随机数组,但不是原始元素的随机数组。下面是如

嗨!如果有人能帮我,我不知道是我的代码逻辑错了还是我犯了一些错误,所以请看一下

我创建了一个名为“多米诺骨牌”的数组,包含13个条目

我用我的“lien”函数将每个dominos条目与html的每个div class=domino链接起来

我用“洗牌”功能洗牌多米诺骨牌阵列

我在文档中发回我的div的新订单

`

const reload=(数组)=>{
设len=array.length-1;

对于(var i=0;i首先,您的所有随机函数都会生成一些随机数组,但不是原始元素的随机数组。下面是如何修复它:

const shuffle = (array) => {
  let len = array.length - 1;
  let dominos = array.slice(0); //make a copy of your original array
   for  ( var i = len; i >= 0 ; i--){
        //select random index from 0 to array length
        var j = Math.floor(Math.random() * (dominos.length - 1));

        /*delete element at selected index from copy of the original array, 
        so it can't be randomly chosen again*/ 
        temp = dominos.splice(j, 1);

        //replace the element in the origin array with the randomly selected one
        array[i] = temp[0]; 
   }
}
const reload = (array) => {

  let len = array.length-1 ;
  for (var i = 0; i <= len; i++) {
    let n = i+1,

     par = document.getElementById('pioche'),
     son = array[i],
     //old = document.getElementById("d"+n); this line makes it all wrong
     old = par.childNodes[i]; //this way you are always addressing the correct child
     par.replaceChild(son, old);

  }} 

lien(dominos);
shuffle(dominos);
reload(dominos)
console.log(dominos);
第二个问题是使用getElementById重新加载函数中选择元素。在进行替换时,可能会添加与DOM树中已有id相同的元素。之后,根据文档,您的id不再唯一:。因为id是e不是唯一的,getElementById的行为是不可预测的。但是您可以始终使用父元素中的childNodes来处理子元素,这就是您可以修复它的方法:

const shuffle = (array) => {
  let len = array.length - 1;
  let dominos = array.slice(0); //make a copy of your original array
   for  ( var i = len; i >= 0 ; i--){
        //select random index from 0 to array length
        var j = Math.floor(Math.random() * (dominos.length - 1));

        /*delete element at selected index from copy of the original array, 
        so it can't be randomly chosen again*/ 
        temp = dominos.splice(j, 1);

        //replace the element in the origin array with the randomly selected one
        array[i] = temp[0]; 
   }
}
const reload = (array) => {

  let len = array.length-1 ;
  for (var i = 0; i <= len; i++) {
    let n = i+1,

     par = document.getElementById('pioche'),
     son = array[i],
     //old = document.getElementById("d"+n); this line makes it all wrong
     old = par.childNodes[i]; //this way you are always addressing the correct child
     par.replaceChild(son, old);

  }} 

lien(dominos);
shuffle(dominos);
reload(dominos)
console.log(dominos);
const reload=(数组)=>{
设len=array.length-1;

对于(var i=0;我非常感谢你的闪电!!我认为随机技术有一个小问题,原因是你的“补丁”我总是在第一个位置得到“13”,而其他人改变了。所以为了解决随机问题,我在我的html“pioche”末尾添加了一个空div:`12 13`和我数组末尾的一个空位置`var dominos=新数组(“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“11”、“12”、“13”);`并且它修复了非随机的第一位情况!非常感谢您的回答对我很有帮助!