我如何转发引用调用事件监听器的元素的 this
范围?
例子:
<input type=button value="Foo" id=mybutton>
addEvent('mybutton','touchstart', function(){
if(window['touchmoved']==false)
hover();
});
function hover(){
this.value //undefined
}
最佳答案
使用 JavaScript Function.call
或 Function.apply
方法:
addEvent('mybutton','touchstart', function(){
if(window['touchmoved']==false)
hover.call(this); // `this` of the called hover points to this `this`
});
关于javascript - 如何将 'this' 范围转发到 javascript 中的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8555372/