3

如何设置 input 框为不可编辑状态 (四种方法)

 3 years ago
source link: https://segmentfault.com/a/1190000040562337
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何设置 input 框为不可编辑状态 (四种方法)

发布于 8 月 23 日

方法 一 : readonly 规定输入字段为只读可复制,但是,用户可以使用Tab键切换到该字段,可选择,可以接收焦点,还可以选中或拷贝其文本。(状态: 输入框会变成灰色)

<input type="text" value="hello JavaScript" readonly="readonly">

方法 二 : disabled 被禁用的 input 元素可复制,不能接收焦点,设置后文字的颜色会变成灰色。无法与 一起使用。 (状态: 输入框会变成灰色)

<input type="text" value="hello JavaScript" disabled="disabled">

方法 三 : 通过控制input的max length为0实现

<input type="text" maxlength="0">

方法 四 : οnfοcus="this.blur();“onfocuse是聚焦的意思,当你把光标放在文本框上输入的时候,就是聚焦,但这里添加了"this.blur()”,blur的作用就是去除聚焦,也就是你不能把光标放在这个文本框上,换句话说就是你不能输入文本了 (状态: 输入框无变色)

<input type="text" value="hello JavaScript" οnfοcus="this.blur();">

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK