如何动态更改一个元素所属的class属性呢?比如说,当一个表格正常情况下背景颜色是白色,鼠标放上去时要变成灰色,这是可以用this.bgColor来更改,但如果要更改的属性不止背景颜色呢?可以用this.className来实现
例子:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>动态更改class属性</title>
<style type="text/css">
<!--
.inp {
background-color: #333333;
}
.inp1 {
background-color: #ffffff;
}
-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input type="text" name="textfield" class="inp" onFocus="this.className='inp1'" onBlur="this.className='inp'">
<input type="text" name="textfield2" class="inp" onFocus="this.className='inp1'" onBlur="this.className='inp'">
</form>
</body>
</html>