常用文本框失去焦点事件、获得焦点事件方法:
例子:
2,使文本框获得焦点,并改单击文本框后,改变背景颜色代码:
css样式:
JS语句,文本框焦点。
var currentlyActiveInputRef=false;
var currentlyActiveInputClassName=false;
function higlightActiveInput(){
if(currentlyActiveInputRef)
{
currentlyActiveInputRef.className=currentlyActiveInputClassName;
}
currentlyActiveInputClassName=this.className;
this.className=’inputHighlighted’;
currentlyActiveInputRef=this;
}
function initInputHighlightScript(){
var tags=[’input’,’textarea’];
for(tagCounter=0;tagCounter<tags.length;tagCounter++){
var inputs=document.getElementsByTagName(tags[tagCounter]);
for(var no=0;no<inputs.length;no++)
{
if(inputs[no].className && inputs[no].className==’doNotHighlightThisInput’) continue;
if(inputs[no].tagName.toLowerCase()==’textarea’||(inputs[no].tagName.toLowerCase()==’input’ && inputs[no].type.toLowerCase()==’text’))
{
inputs[no].onfocus=highlightActiveInput;
inputs[no].onblur=blurActiveInput;
}
}
}
}
<script type="text/javascript" src="highlight-active-input.js"></script>
<form method="post" action="#">
<fieldset>
<legend>Highlight active input</legend>
<table>
<tr>
<td><label for="name">Name:</label></td>
<td><input class="textInput" type="text" name="name" id="name"></td>
</tr>
<tr>
<td><label for="email>E-mail:</label></td>
<td><input class="textInput" type="text" name="email" id="email"></td>
</tr>
<tr>
<td><label for="comment">Comments:</label></td>
<td><textarea id="comment" name="comment" rows="3"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" onclick="return false" value="Submit">
</td>
</tr>
</table>
</fieldset>
</form>
4,脚本放在网页底部