Get height of image inside a hidden div

前端 未结 9 646
自闭症患者
自闭症患者 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

    You can't hide it if you want the correct height and width, but you can move it such that it will be out of the screen, and thus effectively hidden. Try this:

    var hiddenDiv = $('
    ').appendTo('body').css({ 'position': 'absolute', 'top': -9999 }); var img = hiddenDiv.children('img'); img.height(); img.width();

提交回复
热议问题