disabled 属性规定应该禁用 input 元素。
被禁用的 input 元素既不可用,也不可点击。可以设置 disabled 属性,直到满足某些其他的条件为止(比如选择了一个复选框等等)。然后,就需要通过 JavaScript 来删除 disabled 值,将 input 元素的值切换为可用。
以下三种写法都可以禁用 input
1
2
3
|
<
input
type
=
"text"
disabled
=
"disabled"
value
=
"已禁用"
/>
<
input
type
=
"text"
disabled
=
"disabled"
value
=
"已禁用"
/>
<
input
type
=
"text"
disabled
=
"disabled"
value
=
"已禁用"
/>
|
被禁用的 input 默认显示灰色,可以通过CSS修改样式。注:IE9及以下无法改变字体颜色
1. 利用CSS3 :disabled 伪元素定义
1
2
3
4
5
6
|
//Chrome Firefox Opera Safari
input:disabled{
border
:
1px
solid
#DDD
;
background-color
:
#F5F5F5
;
color
:
#ACA899
;
}
|
2. 利用属性选择符定义
1
2
3
4
5
6
|
//IE
6
failed
input[disabled]{
border
:
1px
solid
#DDD
;
background-color
:
#F5F5F5
;
color
:
#ACA899
;
}
|
3. 利用class来定义,为要禁用的input增加一个class
1
2
3
4
5
|
input.disabled{
border
:
1px
solid
#DDD
;
background-color
:
#F5F5F5
;
color
:
#ACA899
;
}
|
最终结果:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
//Chrome Firefox Opera Safari IE
9
+
input:disabled{
border
:
1px
solid
#DDD
;
background-color
:
#F5F5F5
;
color
:
#ACA899
;
}
//IE
8
-
input[disabled]{
border
:
1px
solid
#DDD
;
background-color
:
#F5F5F5
;
color
:
#ACA899
;
}
//IE
6
Using Javascript to add CSS class
"disabled"
* html input.disabled{
border
:
1px
solid
#DDD
;
background-color
:
#F5F5F5
;
color
:
#ACA899
;
}
|
注意:IE8 bug
由于IE8 不识别 :disabled 导致input[disabled],input:disabled样式失效,可以考虑单独来写,或者直接使用input[disabled]。;IE9及以下无法改变字体颜色。