使用js 给页面组件添加class 或者删除class,以及使用jquery的方式添加或修改。
1
2
3
4
5
6
7
8
9
10
11
|
<input id=
"txtBianCeng"
type=
"text"
/>
.lezhu99 {
color:
#cc0000;
}
添加或修改样式(Class)
document.getElementById(
"txtBianCeng"
).className =
"lezhu99"
;
删除样式(Class)
document.getElementById(
"txtBianCeng"
).className =
""
;
|
需要注意的是,在 JavaScript 中,样式使用的是 className 去访问 class 属性,非 class,因为 class 是一个保留关键字,谨记这一点。
如果是使用 jQuery修改标记的样式(Class) 就不一样了,它是使用 removeClass 和 addClass 方法进行删除样式和添加(修改)样式的。