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

前端 未结 6 1076
误落风尘
误落风尘 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:14

    Depends on what browser. Occasionally I find something will break with the synchronous rendering expectations of the javascript runtime and I'll need to get the result on the next tick.

    I'd try:

    $(elem).html(data);
    setTimeout(function(){
        // Get the width here
    },0);
    

提交回复
热议问题