我有一个FlowPanel,其高度是固定的(实际上是其父级高度的百分比,该高度是固定的)。在此面板中,我添加了几个div元素。使用 CSS,我将其高度设置为其父级的 100%。我想要做的是使用 javascript 将其宽度设置为等于其高度。
FlowPanel
div
我遇到的问题是我不知道何时运行这段 javascript。我尝试将其添加到onLoad()我的容器的方法,但高度还不知道(getOffsetHeight()返回 0)。
onLoad()
getOffsetHeight()
我看过类似的问题(比如GWT - 检索未显示的小部件的大小 https://stackoverflow.com/questions/2692097/gwt-retrieve-size-of-a-widget-that-is-not-displayed),但它们并不完全相同。
将呼叫推迟至getOffsetHeight()直到事件循环结束:
@Override public void onLoad() { Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { // Get and work with the element's offsetHeight. } } }