---
title: "使文本框不可编辑[不太常用的方法]"
author: "uule"
date: "2012-09-10T11:17:47Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2012-09-17T17:39:22Z"
id: 1674898
url: "https://uule.iteye.com/blog/1674898"
---

为了使本框不可编辑，我们可以使用下面几种方法 解决思路： 文本框的内容只用来显示而不允许用户随意编辑的时候是很有必要。

方法一：设置&lt;input&gt;标签的readonly属性为true。  
 &lt;input type="text" value="readonly" readonly=“true"/&gt;  

方法二：设置disabled属性为true。  
disabled掉后字会变灰，因此第三种方法比较好。

方法三：在对象focus时立刻让它blur，使它无法获得焦点。  
&lt;input type="text"  **onfocus="this.blur()"** &gt;  

onfocus当对象获得焦点时触发。  
focus使对象得到焦点。  
onblur在对象失去输入焦点时触发。  
blur模糊对象的内容以便使其看起来失去焦点。

注意：readonly和disabled的区别在于后者完全禁止与设置该属性的对象交互(表现为不可改写、不可提交等)。 readonly是可以提交的。