I think document.getElementById
是一个函数。
所以这个函数可以赋值给变量。像这样
var hello = document.getElementById;
console.log(hello('hello')));
<div id="hello">hello</div>
但出现了这样的错误:
未捕获的类型错误:非法调用
问题是上下文。当您引用该函数时,您会丢失该函数的上下文document
。所以,为了做你想做的事,你需要bind
上下文:
var hello = document.getElementById.bind(document);
工作示例:
var hello = document.getElementById.bind(document);
console.log(hello('hello'));
<div id="hello">hello</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)