naturalWidth-类型“ HTMLElement”上不存在属性“ naturalWidth”

use*_*283 0 jquery typescript

我正在尝试在打字稿中访问“ naturalWidth”,但是如果遇到编译错误或在“ naturalWidth”下出现红线。因为$(“#contentImg”)。height())在JavaScript和TypeScript中为我返回0,所以我无法操作图像。图像尺寸不一。如果我用javascript做就可以了。我试过了;

  document.getElementById('contentImg').naturalWidth;
  $("#contentImg")[0].naturalWidth.
Run Code Online (Sandbox Code Playgroud)

这适用于JavaScript,但不适用于TypeScript。有没有一种方法可以获取naturalWidth / Height?

Nit*_*mer 5

的document.getElementById返回一个HTML元素缺乏的naturalWidth特性。
您正在寻找的是具有此属性HTMLImageElementnaturalWidth

let image =  document.getElementById('contentImg') as HTMLImageElement;
console.log(image.naturalWidth); // should be valid
Run Code Online (Sandbox Code Playgroud)