const el: HTMLElement = document.querySelector('.tip');
el.onmouseout = function() {
this.scrollLeft += 10;
};
错误消息,TS2339:GlobalEventHandlers的类型不存在scrollLeft。 this
只是指向el
,为什么错了?
打字稿:3.7.0,3.5.2
尝试此代码:
const el: HTMLElement | null = document.querySelector('.tip');
if (el) {
el.onmouseout = () => { el.scrollLeft += 10;};
}
问题是JS中的所有功能都可以称为白色更改上下文,例如:el.onmouseout.call(newContext, arg1, arg2, ...)
或el.onmouseout.apply(newContext, [arg1, arg2, ...])
。因此,您要做的是创建函数,然后将其分配给el
属性,因此该函数对其上下文一无所知,这就是您出错的原因。