onFocus:在输入框获得焦点后
以下两个JavaScript事件写在html标记中:
使用jQuery的实现方法如下。
元素的焦点事件可以使用jQuery焦点函数focus (,blur )。
focus () :获得焦点时使用,blur ) ) :失去焦点时使用。
代码是下一个:
$(input ).focus ); 或$(input ) ).focus ) function ) (在此为获得焦点时的事件) )
$(input ).blur ); 或$(input ) ).blur ) function ) (这里是失去焦点时发生的事情) ) ) ) ) )。
也可以使用jQuery提供的:focus伪类来确定元素是否获得了焦点。
//Get the focused element:
var$focused=$(:focus );
//No jQuery:
var focused=document.active element;
//Does the element have focus:
varhasfocus=$(input ) ).is ) :focus );
//No jQuery:
elem==elem.owner document.active element;
写个例子吧。
用Ctrl C复制代码
New Document .bor{
border:3px solid red;
}
$ (document.ready (function ) )。
$'input'.focus(function () ) ) ) ) )。
$(this ).addclass('bor );
);
$'input'.blur(function () ) ) ) ) ) )。
$(this ).removeclass('bor );
);
);
用Ctrl C复制代码
当然,根据检索到的元素的状态,还可以更改其他元素的属性和需要发生的事件