如何在变量中缓存HTMLElement.focus?

时间:2017-01-20 11:26:48

标签: javascript dom function-binding

将HTMLElement的focus函数存储在变量中的正确上下文是什么?

我试过了,

var elem = document.getElementById('elem');
var focus = elem.focus.bind(document); // focus() Illegal Invocation
var focus2 = elem.focus.bind(elem); // focus2() Illegal Invocation

1 个答案:

答案 0 :(得分:-1)

将其包装到您自己的功能中:

var elem = document.getElementById('elem');
var focusElem = function(){ elem.focus(); };
focusElem();