JavaScript獲取圖片的大小

字號:


    JavaScript獲取圖片的大小,具體代碼如下:
    <img src="" id="imageE">
    <script>
    function getImageSize(imgE){
    var i = new Image(); //新建一個圖片對象
    i.src = imgE.src;//將圖片的src屬性賦值給新建的圖片對象的src
    return new Array(i.width,i.height);
    }
    </script>
    調用方式:
    <script>
    var imageE = document.getElementById("imageE"),
    imageSize = getImageSize(imageE),
    imgwidth = imageSize[0],
    imgheight = imageSize[1];
    測試方式:
    console.log(imgwidth);
    console.log(imgheight);
    </script>