有没有jQuery散焦方法?


193

如何取消文本区域或输入的焦点?我找不到$('#my-textarea').unfocus();方法?


也不.focusout()是与blur() api.jquery.com/focusout稍有不同的jQuery函数,引用文档This is distinct from the blur event in that it supports detecting the loss of focus on descendant elements (in other words, it supports event bubbling)
Adrien Be

Answers:


335
$('#textarea').blur()

文档位于:http : //api.jquery.com/blur/


奇怪。我试图在窗口失去焦点之前使用blur(),以便当我回来时,默认情况下未选择文本区域。似乎无效:(
Alec Smart

像$('window')。blur(function(){$('#textarea')。blur();})这样的东西;
Alec Smart

也许您需要模糊窗口焦点上的文本区域?
杰夫(Geoff)

7
可能是您正在尝试在加载DOM之前绑定事件。尝试将代码放置在页面的就绪处理程序中,如下所示:$(document).ready(function(){$('#textarea')。blur()})

搭配使用$('#textarea').bind('blur', function() ...)也很棒
Fedir RYKHTIK 2012年

10

根据您的问题,我相信答案是如何触发模糊,而不仅仅是(甚至)设置事件:

 $('#textArea').trigger('blur');

这个答案对我来说更有意义。我想知道如何取消突出显示或使我的文本输入不集中。我知道.blur()存在,但我并不真正了解此用法的正确语法。+1
Partack

7
没有参数,.blur().trigger("blur") api.jquery.com/blur
andreszs 2015年


0

这对我有用:

// Document click blurer
$(document).on('mousedown', '*:not(input,textarea)', function() {
    try {
        var $a = $(document.activeElement).prop("disabled", true);
        setTimeout(function() {
            $a.prop("disabled", false);
        });
    } catch (ex) {}
});

0

我喜欢以下方法,因为它适用于所有情况:

$(':focus').blur();

-12

所以你可以这样做

$('#textarea').attr('enable',false)

试试看并提供反馈


11
它将禁用文本区域,而不是使焦点不集中。
Kurotsuki 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.