jQuery 如何设置元素不可编辑? 下面本篇文章给大家介绍一下使用 jQuery 设置元素不可编辑的方法. 有一定的参考价值, 有需要的朋友可以参考一下, 希望对大家有所帮助.
jQuery 设置页面元素不可点击, 不可编辑, 只读 (备忘) 代码
- $("input").attr('readonly', true);
- $("textarea").attr('readonly', true);
- $(':radio').attr('disabled', true);
- $(':checkbox').attr('disabled', true);
- $(':button').attr('disabled', true);
- $('a').removeAttr('onclick');
- $('select').attr('disabled', true);
在 jQuery 中, 可以通过使用 attr()给元素 (input,textarea,a,select 等等) 设置 disabled 属性, 来禁用该元素, 被禁用的元素, 不可编辑, 不可复制, 不可选择.
例如: 可以给 input 元素添加 disabled 属性, 来禁用 input 元素, 被禁用的 input 元素, 不可编辑.
- <!DOCTYPE html>
- <HTML>
- <head>
- <meta charset="UTF-8">
- <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
- </script>
- </head>
- <body>
- <input type="text" value="不可编辑" />
- </br>
- </br>
- <script type="text/javascript">
- $("input").on('click',
- function() {
- $(this).attr("disabled", true);
- // 或者
- //$(this).attr("disabled", "disabled");
- });
- </script>
- </body>
- </HTML>
说明:
disabled 属性禁用了 input 元素, 被禁用的 input 元素, 不可编辑, 不可复制, 不可选择, 不能接收焦点, 后台也不会接收到传值. 设置后文字的颜色会变成灰色.
注: disabled 属性无法与 <input type="hidden"> 一起使用.
更多 web 前端开发 https://www.html.cn/ 知识, 请查阅 HTML 中文网 !!
来源: http://www.css88.com/qa/jquery/16880.html