使用jQuery如何检测用户取消输入框的焦点?

4

使用jQuery是否可以检测用户取消输入框焦点?即当他们点击其他地方时触发事件。

2个回答

8
我相信你要找的是 blur 函数。
$( "input[type=text]" ).blur( function() {
    // unfocus event
});

1

blur事件会在元素失去焦点时触发。

如果您想确定页面上任何输入元素何时失去焦点,请使用input作为选择器。

$("input").blur(function() {
    //This event fires every time any input element on the page loses focus
});

如果您只是想确定页面上特定输入元素失去焦点的时间,那么使用该元素的id作为jQuery选择器是最有效的。
$("#exampleID").blur(function() {
    //This event fires if the element with an id of "exampleID" loses focus
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接