jsp网页代码:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/txtreadonlycolor.js" type="text/javascript"></script>
<title>文本框背景色变灰Demo</title>
</head>
<body onload="checkreadonly()">
<form name="input" action="" method="get">
<div>
<input name="testtrue" type="text" readonly="true" />
<input name="testnone1" type="text" />
<input name="testnone2" type="text" />
</div>
</form>
</body>
</html>
js文件中javascript代码
function checkreadonly()
{
var txt=document.getElementsByTagName("input");
for(var i=0;i<txt.length;i++)
{
var txttype=txt[i].type;
var txtstyle=txt[i].style;
var txtname=txt[i].name;
if(txttype=="text")
{
if( txtname.readonly = "true")
{
txtstyle.backgroundColor="#EBEBE4";
}
else
{
txtstyle.backgroundColor="white";
}
}
}
}
结果图(本来第一个只读文本框的背景色应该变成灰色的,结果其他两个非只读文本框也变成灰色的了):
求高手指点,不胜感激!!!