Javascript:无法获取新创建的元素的高度 [英] Javascript: unable to get height of newly created element

查看:98
本文介绍了Javascript:无法获取新创建的元素的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新元素(e.i. body.append(html))更新DOM后,我无法立即获取新更新的元素(例如body.height())的高度。部分修复是设置时间间隔,并在时间间隔结束时获取高度(setTimeout(alert(body.height),500))。

After updating the DOM with a new element (e.i. body.append(html)), I cannot immediately get the height of the newly updated element (e.g. body.height()). A partial fix is setting a time interval and getting the height at the end of time interval (setTimeout("alert(body.height)", 500)).

有什么更好的想法?像DOM完全更新后的回调?谢谢。

Any better ideas? Like a callback after DOM completely updates? Thanks.

编辑:
只有当后台有很多事情发生时才会发生(即首页加载时)。如果没有处理任何其他内容,似乎页面更新几乎是即时的。我需要的是一个回调,或者是DOM改变的指标!

This only happens when there is a lot going on in the background (i.e. when page first loads). It would seem like the page update is almost instantaneous if nothing else is being processed. What I need is a callback, or an indicator of when the DOM is reformed!

编辑:
问题是从浏览器被其他的分心进程,这使得浏览器在元素追加之后不能立即更新。如何解决这个问题?

The problem is from the browser being 'distracted' by other processes, which makes the browser unable to update immediately after the append of the element. How do I fix this?

推荐答案

超时方法的作用是因为渲染引擎有机会在那里显示新元素给它一个变化来渲染它,从而赋予它一个高度。

The timeout method works because the rendering engine is given a chance to display the new element there by giving it a change to render it and thus assigning it a height.

你可以将超时设置为0并且仍然具有相同的效果。

You can set the timeout to 0 and still have the same effect.

这篇关于Javascript:无法获取新创建的元素的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆