17

Many DOM elements are focusable: divs with tabIndex, input elements, etc. Is there any simple way to check whether an element is focusable than checking a zillion of different cases? Is there a jQuery method for this?

4

1 回答 1

8

从这里回答“翻译”:哪些 HTML 元素可以接收焦点?

  • <a><area>href
  • 任何未禁用的表单元素
  • 内嵌框架
  • 任何元素tabindex

另外,我相信隐藏的元素也无法获得焦点。

假设条件,以下函数可能会帮助您(假设它总是会收到一个 jQuery 元素):

function canFocus( $el ) {
    if ( $el.is( ":hidden" ) || $el.is( ":disabled" ) ) {
        return false;
    }

    var tabIndex = +$el.attr( "tabindex" );
    tabIndex = isNaN( tabIndex ) ? -1 : tabIndex;
    return $el.is( ":input, a[href], area[href], iframe" ) || tabIndex > -1;
}
于 2013-08-15T21:06:58.850 回答