javascript遍历textbox取背景色问题 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,为什么这个方法没有效果? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <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> c[i].style.backgroundColor 返回的是rgb(164,224,231),要转化成#+16进制的形式 在火狐下是这样的。在ie下是小写的 #a4f4e7 最好还是为相同背景色的设置成同一种形式的id通过id来取值 c[i].style.backgroundColor打出来看一下就知道为什么了! [i].style.backgroundColor== "#A4F4E7 "这是样式不是属性啊!~~改成[i].bgColor="#A4F4E7" 1.text后面貌似多了空格2.type=""也是textbox3.颜色在某些浏览器上会返回rgb4.如果是写在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"))) 单机选中改变样式 javascript验证日期格式的问题 打印时的文本框有下拉框,如何把所有文字全部打印出来,或者直接设置文本框能根据文字自动增长。 如何让层动态显示在不同位置上 用javascript实现连接远程服务器上的access数据库 js取checkbox的值出现的问题 请各位高手帮忙..<三联选单> 谢谢!!! 当一打开网页的时候就全部显示的问题。 帮帮忙,javascript可不可以调用ie中的发送 easyui 我这段代码很寄怪,为什么实行两个就不实行了呢? url上获取json对象问题 JS的兼容问题
<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>
通过id来取值
改成
[i].bgColor="#A4F4E7"
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")))