Get height of image inside a hidden div

前端 未结 9 618
自闭症患者
自闭症患者 2021-01-02 18:50

i want to to get the height/width of an image inside a hidden div-containter, but .height() and .width() both returns 0 (like expected).

         


        
9条回答
  •  长发绾君心
    2021-01-02 19:02

    I'm not sure you can do it with jQuery (or javascript at all). I had a similar problem with misreported heights and widths on loading/hidden img's. You can, however, wait till the image loads, get it's correct height and width and then hide the parent... like so:

    var height, width;
    
    $('#myimg').load(function() {
        height = $(this).height();
        width = $(this).width();
        $('#init').hide();
    }
    

提交回复
热议问题