在js(jquery)中获得文本框焦点和失去焦点的方法
更新时间:2012年12月04日 21:37:58 作者:
文章介绍两个方法和种是利用javascript onFocus onBlur来判断焦点和失去焦点,加一种是利用jquery $("p").blur(); 或$("p").blur(fn)来实现,有需要的朋友可以参考一下
先来看javascript的直接写在了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)
实例
复制代码 代码如下:
<form>
<label for="searchKey" id="lbSearch">搜神马?</label> 这里label覆盖在文本框上,可以更好的控制样式
<input id="searchKey" type="text" />
<input type="submit" value="搜索" />
</form>
jquery代码
复制代码 代码如下:
$(function() {
$('#searchKey').focus(function() {
$('#lbSearch').text('');
});
$('#searchKey').blur(function() {
var str = $(this).val();
str = $.trim(str);
if(str == '')
$('#lbSearch').text('搜神马?');
});
})
好了相当的不错吧
相关文章
JavaScript中如何让 x == 1 && x == 2 && x == 3 等式成立
这篇文章主要介绍了JavaScript中如何让x==1&&x==2&&x==3等式成立,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-07-07


最新评论