使用jQuery检测HTML5的自动聚焦功能

wst*_*str 3 jquery html5 autofocus

每当文本字段集中在我的网站上时,我都会使用jQuery执行操作.只要我通过点击它手动聚焦文本字段,一切正常.

但是:我正在使用HTML5的自动聚焦功能,并且jQuery没有检测到文本字段自动被聚焦.如何手动或自动聚焦文本字段,如何确保我的脚本执行操作?

现在,我正在使用......

$("input:text, input:password").focus(function() { });
Run Code Online (Sandbox Code Playgroud)

...检测文本字段何时突出显示.

Jak*_*ake 7

如果您只需要担心初始页面加载时的自动对焦,那么可能是这样的?

在pageload上触发autofocus'd元素上的焦点事件:

<script>
$(document).ready( function () {
  $("input:text, input:password").focus(function () { });//attach the focus event

  $('input[autofocus]').trigger('focus');//force fire it on the autofocus element
});
</script>
Run Code Online (Sandbox Code Playgroud)