用javascript禁止文本框被输入、被粘贴

目的:自由控制文本框不能被客户改变(只能通过脚本来改变值),或者可以被客户自由改变。

<!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>
    <title></title>
    <script>
        function $(id) {
            return document.getElementByIdx_x(id);
        }
        function empty()
        { }
    </script>
</head>
<body>
姓名:<input type="text" onfocus="this.blur()" onchange="if(this.title) this.value=this.title" title="" onselect="this.title=this.value;" id="tt" /><input type="button" value="设置" onclick="this.previousSibling.value='通过按钮设置得'" />
<input type="button" value="改变" onclick="$('tt').onfocus=empty;$('tt').onchange=empty;$('tt').onselect=empty;$('tt').setAttribute('title','empty')" />
<input type="button" value="获取" onclick="alert($('tt').getAttribute('title'))" />
</body>
</html>

 

缺点:1.借用了title属性

      2.可能存在其它的bugg

优点:这样设置(作为代替readOnly、disabled的属性的方法修补),可以被.net后台获取到值。

原文地址:https://www.cnblogs.com/Denny_Yang/p/1961387.html