使用javascript设置背景颜色适用于每个浏览器,但Chrome

时间:2015-04-15 22:16:58

标签: javascript google-chrome

我使用以下功能允许网站用户点击灰色椭圆形,并在他们选择'时看到带有白色复选标记的靛蓝椭圆。当它们取消选择时,它会回到灰色的椭圆形。它。
除了Chrome 42.0.2311.90之外,它的工作方式与我想要的每个浏览器完全相同。在Chrome上,选择'但是,当第二次点击它时,背景颜色不会从靛蓝变为深灰色,以取消选择'它。

<script type=\"text/javascript\">

function clickeditem(checkaccount) {
    if (document.getElementById(checkaccount).style.backgroundColor==\"indigo\"){
        document.getElementById(checkaccount).style.backgroundColor=\"darkgray\";
    } else {
        document.getElementById(checkaccount).style.backgroundColor=\"indigo\";
    }
    if (document.getElementById(checkaccount).style.color==\"white\"){
        document.getElementById(checkaccount).style.color=\"darkgray\";
    } else {
        document.getElementById(checkaccount).style.color=\"white\";
    }
}

</script>

2 个答案:

答案 0 :(得分:1)

Chrome 42.0.2311.90似乎弄乱了命名颜色的外壳,因为添加.toLowerCase()解决了提问者的问题。
我的小提琴为他工作的提问者commentedhttp://jsfiddle.net/99550tLc/

这是一个堆栈片段(它也简化了代码并使DOM更容易):

function clickeditem(checkaccount){
    checkaccount=document.getElementById(checkaccount);
    if(checkaccount.style.color.toLowerCase() === 'white'){
       checkaccount.style.color = checkaccount.style.backgroundColor = 'darkgray';
    } else {
       checkaccount.style.backgroundColor = 'indigo';
       checkaccount.style.color = 'white';
    }
}
#tst{
    display: inline;
    width: 1em;
    border-radius:0.4em;
    background-color: darkgray;
    color: darkgray;
}
Select click to select option: 
<div id="tst" onclick="clickeditem('tst');">&#x2714;</div> 

答案 1 :(得分:0)

以下作品,感谢GitaarLAB!

function clickeditem(checkaccount) {
    checkaccount=document.getElementById(checkaccount);
    //change color
        if(checkaccount.style.color.toLowerCase() === 'white'){
           checkaccount.style.color = checkaccount.style.backgroundColor = 'darkgray';
        } else {
           checkaccount.style.backgroundColor = 'indigo';
           checkaccount.style.color = 'white';
        }
    }
相关问题