例子,js代码在input文本框中,控制文本框焦点的获取与失去。
 
复制代码 代码示例:
<input name="pwuser" type="text" id="pwuser" class="input" value="楼盘账号" onBlur="if(this.value=='') this.value='楼盘账号';" onFocus="if(this.value=='楼盘账号') this.value='';" /> 
<input name="pwpwd" type="password" class="input1" value="******" onBlur="if(this.value=='') this.value='******';" onFocus="if(this.value=='******') this.value='';">
再来看jquery实现获取与失去文本框焦点的方法,对于元素的焦点事件,可以使用jQuery的焦点函数focus(),blur()。 
focus():得到焦点时使用,和javascript中的onfocus使用方法相同。 
例子: 
 
复制代码 代码示例:
$("p").focus(); 或$("p").focus(fn)
blur():失去焦点时使用,和onblur一样。 
例子: 
 
复制代码 代码示例:
$("p").blur(); 或$("p").blur(fn)
例子,jquery实现文本框焦点获取与失去呈现不同内容。 
 
复制代码 代码示例:
<form> 
<label for="searchKey" id="lbSearch">搜神马?</label> 这里label覆盖在文本框上,可以更好的控制样式 
<input id="searchKey" type="text" /> 
<input type="submit" value="搜索" /> 
</form> 
<script>
$(function() { 
$('#searchKey').focus(function() { 
$('#lbSearch').text(''); 
}); 
$('#searchKey').blur(function() { 
var str = $(this).val(); 
str = $.trim(str); 
if(str == '') 
$('#lbSearch').text('搜神马?'); 
}); 
}) 
</script>