Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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_Css - Fatal编程技术网

Javascript 为什么这不回到白色?

Javascript 为什么这不回到白色?,javascript,css,Javascript,Css,我正在尝试切换div的背景色。它变为绿色,但不会变回白色 函数更改ClassSelectedDiv{ var currentClass=document.getElementByIdSelectedDiv.className; 如果currentClass=未选中{ document.getElementByIds selectedDiv.removeAttributeclass; document.getElementByIdSelectedDiv.className=选中; }否则{ do

我正在尝试切换div的背景色。它变为绿色,但不会变回白色

函数更改ClassSelectedDiv{ var currentClass=document.getElementByIdSelectedDiv.className; 如果currentClass=未选中{ document.getElementByIds selectedDiv.removeAttributeclass; document.getElementByIdSelectedDiv.className=选中; }否则{ document.getElementByIds selectedDiv.removeAttributeclass; document.getElementByIdSelectedDiv.className=未选中; } } .未选{ 背景色:白色; 宽度:20%; 高度:20px; 边框:薄而实的黑色; 边界半径:4px; 浮动:左; 右边距:6px; 垂直对齐:中间对齐; 线高:20px; } .选定{ 背景颜色:绿色; 宽度:20%; 高度:20px; 边框:薄而实的黑色; 边界半径:4px; 浮动:左; 右边距:6px; 垂直对齐:中间对齐; 线高:20px; } 第一组
更正比较运算符

    function ChangeClass(SelectedDiv)
    {
      var currentClass = document.getElementById(SelectedDiv).className;
    if(currentClass == "Unselected")
    {   
     document.getElementById(SelectedDiv).removeAttribute("class");
     document.getElementById(SelectedDiv).className = "Selected";
    }
    else 
    {
     document.getElementById(SelectedDiv).removeAttribute("class");
     document.getElementById(SelectedDiv).className = "Unselected";
    }
    }

您使用了一个等号=,它不检查两个值是否相等,而是将两个值设置为彼此相等。正确的代码是:

function ChangeClass(SelectedDiv)
    {
      var currentClass = document.getElementById(SelectedDiv).className;
    if(currentClass == "Unselected")
    {   
     document.getElementById(SelectedDiv).removeAttribute("class");
     document.getElementById(SelectedDiv).className = "Selected";
    }
    else 
    {
     document.getElementById(SelectedDiv).removeAttribute("class");
     document.getElementById(SelectedDiv).className = "Unselected";
    }
    }

除了错误使用赋值运算符的明显错误外,您还可以编写得更好

由于要重写类名,因此不需要使用removeAttribute

函数更改ClassSelectedDiv{ var el=document.getElementByIdSelectedDiv; el.className=el.className==未选中?“已选中”:“未选中”; } .未选{ 背景色:白色; 宽度:20%; 高度:20px; 边框:薄而实的黑色; 边界半径:4px; 浮动:左; 右边距:6px; 垂直对齐:中间对齐; 线高:20px; } .选定{ 背景颜色:绿色; 宽度:20%; 高度:20px; 边框:薄而实的黑色; 边界半径:4px; 浮动:左; 右边距:6px; 垂直对齐:中间对齐; 线高:20px; }
Div1if currentClass==未选中{如果currentClass=未选中则不{这还不够代码:/你能发布更多吗?不管怎样,你确定document.getElementByIdSelectedDiv是正确的吗?我想你可以使用document.getElementsByClassnameSelectedDiv并使用==或===代替=;@Messervill OP正在向函数传递一个ID,而不是一个类名。@putvande哦,很抱歉没有注意到这是我已经习惯了一个编译器捕捉这样的东西。。。