用JS实现input输入框里的只读属性

大家好!今天我再写程序时,见到了用js如何使input里的输入框产生只读属性。特意搜索一些资料,网上说的很多,我见到了一个特别的,而且简单,给大家分享一下。
欢迎大家转发,版权归作者所有,转载请注明出处

<!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" />
  <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>

点击只读按钮输入框就会变色,并且只读不写,再点击可写按钮就会撤销只读属性,变得可写,并且颜色也变回来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值