【问题标题】:How to cache HTMLElement.focus in a variable?如何在变量中缓存 HTMLElement.focus?
【发布时间】:2017-06-05 09:05:45
【问题描述】:

将 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

【问题讨论】:

    标签: javascript dom function-binding


    【解决方案1】:

    将其包装到您自己的函数中:

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

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 2011-08-05
      • 2021-02-18
      • 2020-10-09
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      相关资源
      最近更新 更多