在Javascript中无法从数组中正确地获取第二时间值

时间:2018-10-26 11:13:01

标签: javascript html

我有四个框,我从数组项目中选择了4个项目。

我已将随机选择的项目放在4个框内。

box002 类可以在显示的四个框中拖放到相应的编号。然后框中的数字消失。

我的问题是正确放置所有四个框后,第二次值未正确填充,即,从数组**items**中重新选择的值与为存储在{{1}中而显示的**tempimages**不匹配}。

该如何解决?并让第二级工作?

**box002**
var items = [{
    label: '1',
    url: 'https://via.placeholder.com/75x75?text=1'
  },
  {
    label: '2',
    url: 'https://via.placeholder.com/75x75?text=2'
  },
  {
    label: '3',
    url: 'https://via.placeholder.com/75x75?text=3'
  },
  {
    label: '4',
    url: 'https://via.placeholder.com/75x75?text=4'
  },
  {
    label: '5',
    url: 'https://via.placeholder.com/75x75?text=5'
  },
  {
    label: '6',
    url: 'https://via.placeholder.com/75x75?text=6'
  },
  {
    label: '7',
    url: 'https://via.placeholder.com/75x75?text=7'
  },
  {
    label: '8',
    url: 'https://via.placeholder.com/75x75?text=8'
  },
  {
    label: '9',
    url: 'https://via.placeholder.com/75x75?text=9'
  },
  {
    label: '10',
    url: 'https://via.placeholder.com/75x75?text=10'
  }
];

var tempimages = [];
var array2 = [];
array2 = items.slice();

var backimgcount = 1;
var len = array2.length;

var item;
var displaycounter = 0;




var images = ['https://picsum.photos/200/300', 'https://picsum.photos/g/200/300'];
var index = 0;

function buildImage() {
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}

function changeImage() {
  index++;
  if (index >= images.length) index = 0;
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}


function rvalue() {


  elements = document.getElementsByClassName("box");

  elements = document.getElementsByClassName("box");

  for (var i = 0; i < elements.length; i++) {
    elements[i].style.backgroundColor = "#ff66ff";
    //elements[i].borderRadius = "2px";
    elements[i].style.border = "2px solid  #e3a89e ";
    //object.style.borderRadius = "1-4 length|% / 1-4 length|%|initial|inherit"

  }


  ptags = document.querySelectorAll('[name="values"]');
  boxtags = document.getElementsByClassName("box");


  for (var index = 0; index < 4; index++) {
    randomIndex = Math.floor(Math.random() * array2.length)

    item = array2[randomIndex];
    //item = array2.splice(randomIndex, 1);
    
    ptags[index].style.visibility = "visible";
    ptags[index].textContent = item.label;
    boxtags[index].style.backgroundImage = 'url(' + item.url + ')'; 
    tempimages.push({
      data: item,
      index: randomIndex
    });

    ptags[index].dataset.itemIndex = randomIndex;

  }

  var tlen = tempimages.length;


}


function displayAllImages() {
  if (tempimages.length == 0) {

    rvalue();
  }
  //tempimages = tempimages.concat(tempimages.splice(0,2));
  //item = tempimages.splice(0, 1);

  function shuffle(array) {
    var currentIndex = array.length,
      temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;

      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }

    return array;
  }


  var arr = tempimages;
  arr = shuffle(arr);

  item = arr.shift();
  image = document.getElementById('slide');
  image.src = item.data.url;
  image.dataset.itemIndex = item.index;


};

$(function() {

  displayAllImages();

});

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id).classList.add('dashed');
}

function drop(ev) {

  ev.preventDefault();
  var data = ev.dataTransfer.getData("Text");
  var el = document.getElementById(data);


  var x = document.getElementById("slide").dataset.itemIndex;
  var y = ev.target.dataset.itemIndex;



  if (x == y) {
    //el.parentNode.removeChild; 

    ev.currentTarget.style.backgroundColor = 'initial';
    ev.currentTarget.style.border = 'initial';

    var pParagraph = ev.currentTarget.firstElementChild;
    //ev.currentTarget.removeChild(pParagraph);
    pParagraph.style.visibility = "hidden";

    item = this.item;
    var arrayvalue = item.dataindex;
    array2.splice(arrayvalue, 1);


    //alert("sucessfull");
    if (tempimages.length == 0) {
      rvalue();
      hiddenimage();
      //changeImage();
      changeImage();
    }
    displayAllImages();
  } else {
    playAudio2();
    alert("WRONG TIME PLACED");
  }

}
body {
  overflow: hidden;
}

.box {
  width: calc(33.3% - 4px);
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #333;
  border: #000 border-color: #e6e600;
  margin: -2px;
  border-radius: 0%;
  background-color: #99ffff;
}

.box {
  height: 15vh;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background-size:contain;
}

.box002 {
  position: absolute;
  top: 27.3vh;
  left: 72.98vw;
  cursor: pointer;
}

.box002 img {
  width: 14.0vw;
  height: 23.0vh;
  border-radius: 50%;
}
p{
font: "Courier New", Courier, monospace;
font-size:30px;

 color: rgba(0,0,0,0.6);
text-shadow: 2px 8px 6px rgba(0,0,0,0.2),
                 0px -5px 35px rgba(255,255,255,0.3);

color: #005ce6;
text-align: center;
}

1 个答案:

答案 0 :(得分:1)

我什至无法想象您如何使用此代码。在将示例发布到stackoverflow上之前,请尝试将其清除(删除未使用的代码,删除不存在的函数)。

问题:
1)拖动功能,.classlist被注释掉
2)playAudio2被注释掉了
3)hiddenimage功能被注释掉了
4)好的,也许索引应该在p标签上,但是可以从示例中找出来(也许您也应该添加到boxtags中。

因此,在此示例中,您可以看到第二次值正常工作,但是当所有图像完成后,就会出现一些新问题。

    var items = [{
    label: '1',
    url: 'https://via.placeholder.com/75x75?text=1'
  },
  {
    label: '2',
    url: 'https://via.placeholder.com/75x75?text=2'
  },
  {
    label: '3',
    url: 'https://via.placeholder.com/75x75?text=3'
  },
  {
    label: '4',
    url: 'https://via.placeholder.com/75x75?text=4'
  },
  {
    label: '5',
    url: 'https://via.placeholder.com/75x75?text=5'
  },
  {
    label: '6',
    url: 'https://via.placeholder.com/75x75?text=6'
  },
  {
    label: '7',
    url: 'https://via.placeholder.com/75x75?text=7'
  },
  {
    label: '8',
    url: 'https://via.placeholder.com/75x75?text=8'
  },
  {
    label: '9',
    url: 'https://via.placeholder.com/75x75?text=9'
  },
  {
    label: '10',
    url: 'https://via.placeholder.com/75x75?text=10'
  }
];

var tempimages = [];
var array2 = [];
array2 = items.slice();

var backimgcount = 1;
var len = array2.length;

var item;
var displaycounter = 0;




var images = ['https://picsum.photos/200/300', 'https://picsum.photos/g/200/300'];
var index = 0;

function buildImage() {
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}

function changeImage() {
  index++;
  if (index >= images.length) index = 0;
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}


function rvalue() {


  elements = document.getElementsByClassName("box");

  elements = document.getElementsByClassName("box");

  for (var i = 0; i < elements.length; i++) {
    elements[i].style.backgroundColor = "#ff66ff";
    //elements[i].borderRadius = "2px";
    elements[i].style.border = "2px solid  #e3a89e ";
    //object.style.borderRadius = "1-4 length|% / 1-4 length|%|initial|inherit"

  }


  ptags = document.querySelectorAll('[name="values"]');
  boxtags = document.getElementsByClassName("box");

debugger;
  for (var index = 0; index < 4; index++) {
    randomIndex = Math.floor(Math.random() * array2.length)

    item = array2[randomIndex];
    //item = array2.splice(randomIndex, 1);
    
    ptags[index].style.visibility = "visible";
    ptags[index].textContent = item.label;
    boxtags[index].style.backgroundImage = 'url(' + item.url + ')'; 
    tempimages.push({
      data: item,
      index: randomIndex
    });

    ptags[index].dataset.itemIndex = randomIndex;

  }

  var tlen = tempimages.length;


}


function displayAllImages() {
  if (tempimages.length == 0) {

    rvalue();
  }
  //tempimages = tempimages.concat(tempimages.splice(0,2));
  //item = tempimages.splice(0, 1);

  function shuffle(array) {
    var currentIndex = array.length,
      temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;

      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }

    return array;
  }


  var arr = tempimages;
  arr = shuffle(arr);

  item = arr.shift();
  image = document.getElementById('slide');
  image.src = item.data.url;
  image.dataset.itemIndex = item.index;


};

$(function() {

  displayAllImages();

});

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);//.classList.add('dashed');
}

function drop(ev) {

  ev.preventDefault();
  var data = ev.dataTransfer.getData("Text");
  var el = document.getElementById(data);


  var x = document.getElementById("slide").dataset.itemIndex;
  var y = ev.target.dataset.itemIndex;



  if (x == y) {
    //el.parentNode.removeChild; 

    ev.currentTarget.style.backgroundColor = 'initial';
    ev.currentTarget.style.border = 'initial';

    var pParagraph = ev.currentTarget.firstElementChild;
    //ev.currentTarget.removeChild(pParagraph);
    pParagraph.style.visibility = "hidden";

    item = this.item;
    var arrayvalue = item.dataindex;
    array2.splice(arrayvalue, 1);


    //alert("sucessfull");
    if (tempimages.length == 0) {
      rvalue();
      //hiddenimage();
      //changeImage();
      changeImage();
    }
    displayAllImages();
  } else {
    //playAudio2();
    alert("WRONG TIME PLACED");
  }

}
body {
  overflow: hidden;
}

.box {
  width: calc(33.3% - 4px);
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #333;
  border: #000 border-color: #e6e600;
  margin: -2px;
  border-radius: 0%;
  background-color: #99ffff;
}

.box {
  height: 15vh;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background-size:contain;
}

.box002 {
  position: absolute;
  top: 27.3vh;
  left: 72.98vw;
  cursor: pointer;
}

.box002 img {
  width: 14.0vw;
  height: 23.0vh;
  border-radius: 50%;
}
p{
font: "Courier New", Courier, monospace;
font-size:30px;

 color: rgba(0,0,0,0.6);
text-shadow: 2px 8px 6px rgba(0,0,0,0.2),
                 0px -5px 35px rgba(255,255,255,0.3);

color: #005ce6;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container2">
  <div class="containerr">
    <div class="pic" id="content">
      <div id="container">

        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="10">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="11">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="12">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="13">
          <p name="values"></p>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="" draggable="true" id="slide" border="rounded" />
</div>