【问题标题】:Is there a jQuery unfocus method?有没有 jQuery unfocus 方法?
【发布时间】:2009-05-13 10:45:50
【问题描述】:

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

【问题讨论】:

  • 也不是 jQuery 函数 .focusout()blur() api.jquery.com/focusout 略有不同,引用文档 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)

标签: javascript jquery


【解决方案1】:
$('#textarea').blur()

文档地址:http://api.jquery.com/blur/

【讨论】:

  • 奇怪。我试图在窗口失去焦点之前模糊(),这样当我回来时,默认情况下不会选择文本区域。好像不行:(
  • 类似于 $('window').blur(function() { $('#textarea').blur(); });
  • 也许你需要模糊窗口焦点上的文本区域?
  • 可能是您尝试在加载 DOM 之前绑定事件。尝试将代码放在页面的就绪处理程序中,如下所示: $(document).ready(function() { $('#textarea').blur() })
  • $('#textarea').bind('blur', function() ...) 也很好用
【解决方案2】:

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

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

【讨论】:

  • 这个答案对我来说更有意义。我想知道如何取消突出显示或使我的文本输入不集中。我知道 .blur() 存在,但我并不真正理解这种用法​​的正确语法。 +1
  • 不带参数,.blur().trigger("blur")的快捷方式api.jquery.com/blur
【解决方案3】:

猜你在找.focusout()

【讨论】:

【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    这对我有用:

    // 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) {}
    });
    

    【讨论】:

      【解决方案6】:

      所以你可以这样做

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

      尝试并提供反馈

      【讨论】:

      • 它将禁用文本区域,而不是取消焦点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 2012-10-07
      • 2012-02-20
      • 1970-01-01
      • 2012-06-25
      • 2011-02-28
      • 1970-01-01
      相关资源
      最近更新 更多