代码之家  ›  专栏  ›  技术社区  ›  sahal akma

Number()方法没有给我正确的输出[重复]

  •  0
  • sahal akma  · 技术社区  · 3 年前

    我正在创建一个包含图像的React项目。 在我的项目中,我想得到上传图像的宽度和高度。 我用的是

    const image = document.getElementById("inputimage");
    const width = Number (image.width);
    const height = Number (image.height);
    console.log( width, height );
    

    另一页中的图像标签

    <img id='inputimage' alt='image' src={imageUrl} width="500px" height="auto" />
    

    在这里 Number() 用于转换 image.width image.height 转化成数字以备将来计算。 每当我运行这段代码时,我都没有得到输出。

    const image = document.getElementById("inputimage");
    const width = image.width;
    const height = image.height;
    console.log( width, height );
    

    但是当我移除 编号() 然后我得到了输出。

    有人知道为什么我在 编号() 使用了什么? 我能做些什么来转换 形象宽度 形象身高 变成数字?

    1 回复  |  直到 3 年前
        1
  •  0
  •   Odilf    3 年前

    我想你可以用 parseInt(image.width) (或 image.height ).