JavaScript(或 jQuery)中是否有“重点”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2683742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:27:23  来源:igfitidea点击:

Is there a 'has focus' in JavaScript (or jQuery)?

javascriptjqueryjquery-selectors

提问by alex

Is there something I can do like this (perhap via a plugin)

有什么我可以做的吗(也许通过插件)

if ( ! $('form#contact input]').hasFocus()) {
  $('form#contact input:first]').focus();
}

Basically, set focus to the first input, but only if the user has not already clicked into anything?

基本上,将焦点设置为第一个输入,但前提是用户尚未单击任何内容?

I know this will work too, but is there anything more elegant?

我知道这也可以,但还有什么更优雅的吗?

$(function() {
  var focused = false;
  $('form#contact input]').focus(function() {
    focused = true;
  }); 
  setTimeout(function() {
    if ( ! focused) {      
      $('form#contact input:first]').focus();
    }
  }, 500);
});

回答by cletus

There is no native solution but yes there is a more elegant way you can do it:

没有本地解决方案,但是有一种更优雅的方法可以做到:

jQuery.extend(jQuery.expr[':'], {
  focus: "a == document.activeElement"
});

You're defining a new selector. See Plugins/Authoring. Then you can do:

您正在定义一个新的选择器。请参阅插件/创作。然后你可以这样做:

if ($("...").is(":focus")) {
  ...
}

or:

或者:

$("input:focus").doStuff();

回答by dclowd9901

$('input:focus')

$('input:focus')

It's CSS. You don't need to create a "custom selector." It already exists! http://www.w3schools.com/CSS/pr_pseudo_focus.asp

是 CSS。您不需要创建“自定义选择器”。它已经存在了!http://www.w3schools.com/CSS/pr_pseudo_focus.asp

Just attach whatever process you want to do to that selector, and it will weed it out if the element in question is not focused. I did this recently to keep a keyupfrom instantiating an email input error check when the e-mail input wasn't being used.

只需将您想要执行的任何过程附加到该选择器上,如果相关元素未获得焦点,它就会将其清除。我最近这样做是为了防止keyup在未使用电子邮件输入时实例化电子邮件输入错误检查。

If all you're trying to do is check if the user has focused on anything themselves, just do this:

如果您要做的只是检查用户自己是否专注于任何事情,请执行以下操作:

if($('input:focus').size() == 0){
    /* Perform your function! */
}

回答by alex

jQuery 1.6 now has a dedicated :focusselector.

jQuery 1.6 现在有一个专用的:focus选择器。

回答by luiggitama

I had trouble with cletus approach, using jQuery 1.3.2 and Firefox 3.6.8, because the string "a == document.activeElement"was not a valid function.

我在使用 jQuery 1.3.2 和 Firefox 3.6.8 的 cletus 方法时遇到了麻烦,因为该字符串"a == document.activeElement"不是有效的函数。

I fixed it defining a function for the focuskey. In fact, all other keys defined in jQuery.expr[':']are defined as functions. Here's the code:

我修复了它,为focus键定义了一个函数。事实上,在jQuery.expr[':']中定义的所有其他键都定义为函数。这是代码:

jQuery.extend(jQuery.expr[':'], {
    focus: function(e){ return e == document.activeElement; }
});

So, now it works as expected.

所以,现在它按预期工作。

However, I was experiencing some strange behaviour in Firefox 3.6.8 (maybe a bug in FF?). If I clicked on an input text while the page was rendering, and if I called is(":focus")on page load, I would get an error from the browser, reported by FireBug, and the script would break.

但是,我在 Firefox 3.6.8 中遇到了一些奇怪的行为(可能是 FF 中的错误?)。如果我在页面渲染时单击输入文本,并且如果我调用is(":focus")页面加载,我会从浏览器收到 FireBug 报告的错误,并且脚本会中断。

To solve this, I surrounded the code with a try...catchblock, returning falseon error. Use it if you want to prevent your users from experiencing the same error:

为了解决这个问题,我用一个try...catch块包围了代码,false在错误时返回。如果您想防止用户遇到相同的错误,请使用它:

jQuery.extend(jQuery.expr[':'], {
    focus: function(e){
        try{ return e == document.activeElement; }
        catch(err){ return false; }
    }
});

回答by SLaks

No, there isn't.

不,没有。

However, you can simulate it like this:

但是,您可以像这样模拟它:

$(':input')
    .data('focused', false)
    .focus(function() { $.data(this, 'focused', true); })
    .blur(function() { $.data(this, 'focused', false); });

回答by user5185370

Frustratingly difficult to find a solution to this problem considering the solution is actually very simple:

考虑到解决方案实际上非常简单,很难找到解决此问题的方法:

if (document.activeElement == this) {
  // has focus
}

if (document.activeElement != this) {
  // does not have focus
}

回答by ExEdzy

I know this is an old question, but may be my solution will help someone :)

我知道这是一个老问题,但我的解决方案可能会对某人有所帮助:)

since this didnt worked for me:

因为这对我不起作用:

if ($(this)!=$(document.activeElement)) { ... }

..were "this" is returned from blur function. So i did this:

..were "this" 是从 blur 函数中返回的。所以我这样做了:

if ($(document.activeElement).attr("class") != "input_textbox"){ ... }

回答by JohnFx

Here is a succinct way to do it.

这是一个简洁的方法。

$(document.activeElement)

or to plug it into your example..

或将其插入您的示例中..

if ($('form#contact input]')[0]!=$(document.activeElement)) { ... }

回答by Kafoso

    $('*:focus')

(Necro ftw, but still valid and useful)

(Necro ftw,但仍然有效且有用)