Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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/3/reactjs/26.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 随机数不';t工作,仅在第一个元素上显示_Javascript - Fatal编程技术网

Javascript 随机数不';t工作,仅在第一个元素上显示

Javascript 随机数不';t工作,仅在第一个元素上显示,javascript,Javascript,我被我的代码卡住了。所以基本上当我点击第一扇门时,它会打开一个图像。它根据我的情况显示一个图像随机数,例如:1=门是空的,2=你找到那个人 当我单击第二扇门时,第一扇刚刚打开的门将返回原始图像,如果我打开第三扇门,第二扇门将返回原始图像 我的问题是第一扇门是开着的,但是第二扇门和第三扇门都不开。 我想我需要添加“.is selected”,但我不确定如何实现它 但是我不想使用html,而是想使用javascript const doorTarget=document.querySelector

我被我的代码卡住了。所以基本上当我点击第一扇门时,它会打开一个图像。它根据我的情况显示一个图像随机数,例如:1=门是空的,2=你找到那个人

当我单击第二扇门时,第一扇刚刚打开的门将返回原始图像,如果我打开第三扇门,第二扇门将返回原始图像

我的问题是第一扇门是开着的,但是第二扇门和第三扇门都不开。 我想我需要添加“.is selected”,但我不确定如何实现它

但是我不想使用html,而是想使用javascript

const doorTarget=document.querySelector(“.door track”);
const doorClick=Array.from(doorTarget.children);
const door=true;
doorClick.forEach(功能(测试){
test.addEventListener(“单击”),函数(){
如果(门){
const doorRandom=Math.floor(Math.random()*3)+1;
//const currentSelector=doorTarget.querySelector(“.is selected”);
//const image=currentSelector.nextElementSibling;
//currentSelector.classList.remove('is-selected')
//image.classList.add('is-selected')
控制台日志(doorRandom);
如果(门随机===1){
document.querySelector(“.test1”).src=“images/friends.png”;
}否则{
document.querySelector(“.test1”).src=“images/empty room.png”;
}
}
})
})

您可以使用

document.querySelector(".test1").classList.add("is-selected");
添加一个类和

document.querySelector(".test1").classList.remove("is-selected");
再次删除该类

编辑:要获取所有元素,请使用querySelectorAll()并在HTML集合中循环

document.querySelector(".test1").classList.add("is-selected");
添加一个类和

document.querySelector(".test1").classList.remove("is-selected");
再次删除该类


编辑:要获取所有元素,请使用querySelectorAll()并在HTML集合中循环

这里我随意编写了它,请确保阅读注释

const doorTarget=document.querySelector(“.door track”);
const doorClick=Array.from(doorTarget.children);
const door=true;
doorClick.forEach(功能(测试){
设t=test;
test.addEventListener(“单击”),函数(){
//重置所有选定的类,如果不需要,请删除它。我不确定您是否需要它
doorClick.forEach((项目)=>item.querySelector(“img”).classList.remove(“被选中”))
如果(门){
const doorRandom=Math.floor(Math.random()*3)+1;
var img=t.querySelector(“img”);
控制台日志(doorRandom);
//将选定的类添加到图像中
img.classList.add(“已选择”)
//更改图像的src或其他内容
如果(门随机===1){
img.src=“images/friends.png”;
}否则{
img.src=“images/empty room.png”;
}
}
})
})
选择了
{
边框:1px纯红;
}


我在这里写了你喜欢的,一定要阅读评论

const doorTarget=document.querySelector(“.door track”);
const doorClick=Array.from(doorTarget.children);
const door=true;
doorClick.forEach(功能(测试){
设t=test;
test.addEventListener(“单击”),函数(){
//重置所有选定的类,如果不需要,请删除它。我不确定您是否需要它
doorClick.forEach((项目)=>item.querySelector(“img”).classList.remove(“被选中”))
如果(门){
const doorRandom=Math.floor(Math.random()*3)+1;
var img=t.querySelector(“img”);
控制台日志(doorRandom);
//将选定的类添加到图像中
img.classList.add(“已选择”)
//更改图像的src或其他内容
如果(门随机===1){
img.src=“images/friends.png”;
}否则{
img.src=“images/empty room.png”;
}
}
})
})
选择了
{
边框:1px纯红;
}

eventListener()
添加到列表(
等),然后创建if/else条件,其中
s
是对事件做出反应的唯一元素(例如
单击
鼠标盖
,等等)。传递事件对象并使用:

  • 将始终返回事件的起源(例如,单击
    ,鼠标悬停
    ,等等)

  • 是具有
    eventListener()
    的元素

ul{
显示器:flex;
列表样式:无;
}

eventListener()
添加到列表(
等),然后创建if/else条件,其中
s
是对事件做出反应的唯一元素(例如
单击
鼠标盖
,等等)。传递事件对象并使用:

  • 将始终返回事件的起源(例如,单击
    ,鼠标悬停
    ,等等)

  • 是具有
    eventListener()
    的元素

ul{
显示器:flex;
列表样式:无;
}