Javascript classList.remove似乎崩溃了
所以我基本上是在尝试转型。 以下是Chrome给我的错误消息: 未捕获的TypeError:无法读取未定义的属性“remove”Javascript classList.remove似乎崩溃了,javascript,css,Javascript,Css,所以我基本上是在尝试转型。 以下是Chrome给我的错误消息: 未捕获的TypeError:无法读取未定义的属性“remove” document.getElementsByClassName()-返回一个HTMLCollection-一个类似数组的对象 或者使用一个id: document.getElementById("DefuserID") 或使用querySelector仅获取第一个匹配项: document.querySelector("Defuser") 或者通过其索引选择HTM
document.getElementsByClassName()-返回一个HTMLCollection-一个类似数组的对象 或者使用一个id:
document.getElementById("DefuserID")
或使用querySelector仅获取第一个匹配项:
document.querySelector("Defuser")
或者通过其索引选择HTMLCollection中的第一个,如下所示:
document.getElementsByClassName("Defuser")[0];
好的,我的建议是像这样使用
DefuseContainer[0]
// function receiving currentPhase
function showDefuseContainer(currentPhase){
var DefuseContainer = document.getElementsByClassName("Defuser");
if(currentPhase == "defuse" || currentPhase == "over"){
DefuseContainer[0].classList.add("show"); // if currentPhase is defuse || over then add show class means show the Container
}else{
DefuseContainer[0].classList.remove("show"); // else remove the show class and hide the container
}
}
DefuseContainer
是元素列表(注意复数形式的getElementsByClassName)classList
是单个元素的属性。请改为使用:var DefuseContainer=document.querySelector(“.Defuser”)代码>。您当前正在获取items@TylerRoperDefuseContainer
将被列出,因为document.getElementsByClassName(“Defuser”)代码>@dupinersingh对不起,我没听懂。我相信这差不多就是我说的,不?耶,没错,我对你说是的,你是对的,这是DefuseContainer
元素数组
// function receiving currentPhase
function showDefuseContainer(currentPhase){
var DefuseContainer = document.getElementsByClassName("Defuser");
if(currentPhase == "defuse" || currentPhase == "over"){
DefuseContainer[0].classList.add("show"); // if currentPhase is defuse || over then add show class means show the Container
}else{
DefuseContainer[0].classList.remove("show"); // else remove the show class and hide the container
}
}