📌  相关文章
📜  如何获取图像的宽度和高度?

📅  最后修改于: 2022-05-13 01:56:54.964000             🧑  作者: Mango

如何获取图像的宽度和高度?

给定一张图片,任务是使用 JavaScript 获取图片的宽度和高度。 width 和 height 属性用于显示图像的宽度和高度。

宽度的语法:

var width = this.width;

高度的语法:

var height = this.height;

例1:本例选择图片,然后使用this.width和this.height方法获取图片的宽高。



      

    
        Get the real width and height of
        an image using JavaScript
    

  

  
        

            GeeksforGeeks         

           

            Getting the real width and height of an image         

                                   

输出:

  • 在点击按钮之前:
    ngcut
  • 点击按钮后:
    ngcut

示例 2:此示例显示图像的尺寸。它将在不使用警报函数的情况下显示结果。在这里,我们将在同一窗口中显示结果。



  

    
        Get the real width and height of
        an image using JavaScript
    

  

      
    
      
    

        Click the button to display the width         and height of the image     

                       

The width of the image in pixels:

           

           

The height of the image in pixels:

           

                                   

输出:

  • 在点击按钮之前。
    ngcut
  • 点击按钮后:
    ngcut