Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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_Html - Fatal编程技术网

Javascript 仅记录到第一个阵列

Javascript 仅记录到第一个阵列,javascript,html,Javascript,Html,所以我在做一个测验 我已经为每个错误的元素设置了将互文更改为错误的元素 但它只改变了第一个元素,即使我把它设为foreach,为什么 const op1 = document.getElementById("op1") const op2 = document.getElementById("op2") const op3 = document.getElementById("op3") const op4 = document.get

所以我在做一个测验

我已经为每个错误的元素设置了将互文更改为错误的元素

但它只改变了第一个元素,即使我把它设为foreach,为什么

const op1 = document.getElementById("op1")
const op2 = document.getElementById("op2")
const op3 = document.getElementById("op3")
const op4 = document.getElementById("op4")

    let origArray = [op1,op2,op3,op4];
                                    
var math = origArray[Math.floor(Math.random()* origArray.length)]
math.innerText = "Right"

function wrong(){
    var cloneArray = origArray.slice();
var i = math;

const something = cloneArray.splice(i,1);

something.forEach(elem => {
    elem.innerText = "Wrong"
    console.log(elem.id)
});
}

wrong()
你应该这样做

const op1 = document.getElementById("op1")
const op2 = document.getElementById("op2")
const op3 = document.getElementById("op3")
const op4 = document.getElementById("op4")

    let origArray = [op1,op2,op3,op4];
var rightIndex = Math.floor(Math.random()* origArray.length);                                    
var math = origArray[rightIndex];
math.innerText = "Right"

function wrong(){
    var cloneArray = origArray.slice();
var i = rightIndex;

cloneArray.splice(i,1);

cloneArray.forEach(elem => {
    elem.innerText = "Wrong"
    console.log(elem.id)
});
}

wrong()

它没有记录的原因是,它没有遍历所有元素,而是只遍历splice返回的元素

以下是一个更好的方法(imo)来做你想做的事情:

//Get option DOM-element references
const op1 = document.getElementById("op1");
const op2 = document.getElementById("op2");
const op3 = document.getElementById("op3");
const op4 = document.getElementById("op4");

//Make an array with all the listed options
let originalArray= [op1,op2,op3,op4];

//Save a random element "math" as the right answer, and set it's inner text to "Right"                                     
var correctArrayIndex = Math.floor(Math.random()* originalArray.length);
originalArray[correctArrayIndex].innerText = "Right";
//This functions returns a cloned array of the original array with the correct text
function returnClonedArrayWithWrong(inputArray, correctIndex){
    var clonedArray = inputArray.splice();
    for(var i = 0; i<inputArray.length; i++){
        if(i != correctIndex){
            clonedArray[i].innerText = "Wrong";
        }else{
            clonedArray[i].innerText = "Right";
        }
        console.log("Element ID: "+ clonedArray[i].id); // For logging the output
    }
    return clonedArray;
}
var clonedArrayFilled = returnClonedArrayWithWrong(originalArray, correctArrayIndex);
//获取选项DOM元素引用
const op1=document.getElementById(“op1”);
const op2=document.getElementById(“op2”);
const op3=document.getElementById(“op3”);
const op4=document.getElementById(“op4”);
//使用列出的所有选项创建一个数组
设originalArray=[op1,op2,op3,op4];
//将随机元素“math”保存为正确答案,并将其内部文本设置为“right”
var correctarayindex=Math.floor(Math.random()*originalArray.length);
原始阵列[correctArrayIndex].innerText=“右”;
//此函数用于返回具有正确文本的原始数组的克隆数组
函数返回ClonedArrayWithError(inputArray,correctIndex){
var clonedArray=inputArray.splice();

对于(var i=0;i您仅从
splice()
调用返回一项,因此
something
的长度仅为1。嗯,我应该怎么做?而不是“something”返回除“math”之外的每个元素?
splice()
返回已删除的元素,而不是剩余的数组,因此您应该调用
forEach()
on
cloneArray
在拼接调用之后。:“返回值:包含已删除元素的数组。如果仅删除一个元素,则返回一个元素数组。如果未删除任何元素,则返回空数组。”