jQuery: Why does .width() sometimes return 0 after inserting elements with .html()?

前端 未结 6 1064
误落风尘
误落风尘 2021-02-07 00:57

I am getting some html and inserting it with .html(), after which I am trying to get the width of one of newly inserted elements with .width() (which h

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 01:09

    • If the element is an image, then the width() and height() methods should be accessed on the .load() jQuery handler;

    • If the element is a DOM subtree, then they should be accessed on the .ready() handler.

    The reason is that you can't rely on what jQuery reports to be the size of the element until the element has actually been loaded.

提交回复
热议问题