使用Jquery/javascript
是:$(document).focus()吗?
发布于 2010-04-03 04:21:29
如果将函数作为第一个参数传递,则jQuery包装元素上的.focus()方法将绑定到onfocus事件,或者触发没有参数的附加事件处理程序。
我不知道你为什么要做你想做的事情,但是如果你想模糊当前活动的元素,不管它是什么元素,你可以使用:
if ("activeElement" in document)
document.activeElement.blur();不过,我们不建议这样做,因为这样做会重置跳转顺序,并可能会惹恼键盘导航用户。
发布于 2018-09-29 23:15:51
更新: Typescript中的你应该像这样转换成HTMLElement:
(document.activeElement as HTMLElement).blur();
// or
(<HTMLElement>document.activeElement).blur();原始答案如下。
公认的答案在TypeScript中不起作用,因为document.activeElement是Element类型,而不是HTMLElement,所以它在技术上不支持blur()。下面的方法有点老生常谈,但它工作正常。
改编自this answer
const tmp = document.createElement('input');
document.body.appendChild(tmp);
tmp.focus();
document.body.removeChild(tmp);发布于 2010-04-03 04:19:45
$().method()触发与该方法关联的侦听器。如果需要调用dom元素上的函数,请对jQuery对象进行索引。
即
$(document)[0].focus()尽管为什么不直接使用document.focus可能是一个值得回答的问题。
https://stackoverflow.com/questions/2568951
复制相似问题