大家好!今天我再写程序时,见到了用js如何使input里的输入框产生只读属性。特意搜索一些资料,网上说的很多,我见到了一个特别的,而且简单,给大家分享一下。
欢迎大家转发,版权归作者所有,转载请注明出处
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title> New Document </title><meta name="Generator" content="EditPlus"><meta name="Author" content=""><meta name="Keywords" content=""><meta name="Description" content=""></head><script language="JavaScript">function isreadonly(){var obj = document.getElementById("username");obj.setAttribute("readonly",true);obj.style.backgroundColor="#d2d2d2";}function readwrite(){var obj = document.getElementById("username");obj.removeAttribute("readOnly");obj.style.backgroundColor="#ffffff";}</script><body><form name="addform" id="addform" method="post" action="" ><input type="text" id="username" name="username"><input type="button" name="只读" value="read" onclick="isreadonly();"><input type="button" name="可写" value="write" onclick="readwrite();"></form></body></html>
点击只读按钮输入框就会变色,并且只读不写,再点击可写按钮就会撤销只读属性,变得可写,并且颜色也变回来了。