ts Property ‘offsetTop‘ does not exist on type ‘Element‘

ts Property ‘offsetTop’ does not exist on type ‘Element’

problem

ts Property ‘offsetTop’ does not exist on type ‘Element’

reason

Element 类型上没有 offsetTop, HTMLElement 类型上有 offsetTop
可以在类型上点进去看到

solution

// 修改前
const element = document.getElementById('my-element') as Element;
const offsetTop = element.offsetTop;
// 修改后
const element = document.getElementById('my-element') as HTMLElement;
const offsetTop = element.offsetTop;

猜你喜欢

转载自blog.csdn.net/qubes/article/details/130346432
今日推荐