Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.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 - Fatal编程技术网

Javascript 切换未在正确的元素上工作

Javascript 切换未在正确的元素上工作,javascript,Javascript,这段代码的问题在于,无论我点击哪个图标,它总是第一个问题,其他问题都不会。有人能帮忙吗? 我得到了document.querySelector()将始终返回第一个元素,但是如果我使用document.queryselectoral()我会得到一个节点列表和一个错误,上面说:“uncaughttypeerror:无法读取未定义的属性'display'” let openQuestion=()=>{ 让我回答 answer=document.querySelector('.answer') co

这段代码的问题在于,无论我点击哪个图标,它总是第一个问题,其他问题都不会。有人能帮忙吗? 我得到了
document.querySelector()
将始终返回第一个元素,但是如果我使用
document.queryselectoral()
我会得到一个节点列表和一个错误,上面说:“uncaughttypeerror:无法读取未定义的属性'display'”

let openQuestion=()=>{
让我回答
answer=document.querySelector('.answer')
console.log(应答)
if(answer.style.display==“block”){
answer.style.display='none';
console.log(answer.style.display)
console.log('f')
}否则{
answer.style.display='block';
console.log(answer.style.display)
console.log('a')
}
}

我可以退货吗?
我的同僚们都是精英。最大限度地保护动物,保护动物,保护动物,保护动物,保护动物!
阿库桑蒂姆·尼莫·奥特姆公积金。事实上,这是一个由建筑设计师设计的概念,甚至是一个官员的过失!不让它坐在这里,odit

运送我的物品要花多少钱? 我的同僚们都是精英。最大限度地保护动物,保护动物,保护动物,保护动物,保护动物! 阿库桑蒂姆·尼莫·奥特姆公积金。事实上,这是一个由建筑设计师设计的概念,甚至是一个官员的过失!不让它坐在这里,odit


在html和js中的每个问题上添加一个id

let openQuestion = (id) => {
let answer

answer = document.getElementById(id)
...

以下三个示例的重要说明:

您必须确保显示的Javascript代码仅在浏览器解析包含问题和答案的HTML之后执行。这可以通过将保存Javascript的
-标记放在结束正文标记
之前来实现,也可以通过将示例中的JS代码包装在
DOMContentLoaded
侦听器中(浏览器解析完页面中的所有HTML后立即运行)来实现,如下所示:

document.addEventListener('DOMContentLoaded', function() {
  /* the JS from the examples here */
});
我不知道您的HTML,但我将创建一个需要完成的最小示例:

//首先获取所有问题的节点列表
const questions=document.queryselectoral('.question');
//然后迭代该列表
for(常量问题){
//并为每个按钮添加一个单击侦听器
问题.addEventListener('单击',()=>{
//找到属于所单击问题的答案
const answerToQuestion=question.nextElementSibling;
//然后使用“隐藏”属性切换其可见性
回答问题。TogleAttribute(“隐藏”);
})
}
。回答{颜色:绿色;左边距:15px;}

生命的意义是什么?
42
生命有意义吗?
胡特?
福?
酒吧。

document.querySelector('.answer')
将始终返回文档中匹配的第一个元素,并且仅返回该元素。是否可以添加一些示例HTML?@connexo是的,我理解,但如果使用document.querySelectorAll('.answer'),它将返回节点列表,我会收到此错误“app.js:83未捕获类型错误:无法读取未定义的属性'display'“@RMP1992检查随
querySelectorAll
返回的内容-它是一个数组,因此不会有
display
属性,只有数组中的元素才会有它。@maxshuty问题是它没有
style
属性。如果它没有
style
属性,显然您也无法访问
style.display
。这只会让事情变得更糟。谢谢,这是我的代码const questions=document.querySelectorAll('.question container');对于(const-question-of-questions){question.addEventListener('click',()=>{const-answer=question.querySelector('.answer');answer.toggleAttribute('hidden');})但是HTML的隐藏属性没有被移除,它必须是
const-questions=document.querySelectorAll('.questions')。您已选择查找所有
.question container
元素,而不是
.questions
元素。此外,在HTML中,答案是问题的相邻同级。使用nextElementSibling而不是querySelector。仍不工作…我有更改,但仍不工作