var c=document.getElementsByTagName("input");
            var i;
            for(i=0;i<c.length;i++)
            {
                if(c[i].type=="text"&&c[i].style.backgroundColor=="#A4F4E7")
                {
                    alert("aaaaaaaaaaaaaa");
                    return false;
                }
            }
页面中有多个文本框,其中有几个背景色就是#A4F4E7,为什么这个方法没有效果?

解决方案 »

  1.   


    <body>
        <input id="input3" type="text" style="background-color: #A4F4E7" />
        <input id="input2" type="text" style="background-color: #A4F4E7" />
        <input id="input1" type="text" style="background-color: #A4F4E7" />
        <script type="text/javascript">
            (function(){
                var   c=document.getElementsByTagName("input");
                            for(var i=0;i <c.length;i++)
                            {
                                    alert(c[i].style.backgroundColor);
                                    if(c[i].type=="text"&&c[i].style.backgroundColor== "#A4F4E7")
                                    {
                                            alert( "aaaaaaaaaaaaaa");
                                    }
                            } 
            })();
                            
        </script>
    </body>
      

  2.   

    c[i].style.backgroundColor 返回的是rgb(164,224,231),要转化成#+16进制的形式
      

  3.   

    在火狐下是这样的。在ie下是小写的 #a4f4e7
      

  4.   

    最好还是为相同背景色的设置成同一种形式的id
    通过id来取值
      

  5.   

    c[i].style.backgroundColor打出来看一下就知道为什么了!
      

  6.   

    [i].style.backgroundColor== "#A4F4E7 "这是样式不是属性啊!~~
    改成
    [i].bgColor="#A4F4E7"
      

  7.   

    1.text后面貌似多了空格
    2.type=""也是textbox
    3.颜色在某些浏览器上会返回rgb
    4.如果是写在css中,而不是标签的style中,.style并不能取得正确的属性获取当前样式函数:
    function getStyle(target, styleName) {
         if (window.getComputedStyle) {
              return window.getComputedStyle(target, null)[styleName];
         } else {
              return target.currentStyle[styleName];
         }
    }
    综上,条件修改为
    if((c[i].type=="" || c[i].type=="text")&&(getStyle(c[i], "backgroundColor")== "#A4F4E7" || getStyle(c[i], "backgroundColor")=="rgb(164,224,231")))