开发者

Get width of element after resizing it

开发者 https://www.devze.com 2022-12-27 12:39 出处:网络
Is it possible to get the size of an element after resizing it using $(el).css()? My code is: function resize() {

Is it possible to get the size of an element after resizing it using $(el).css()?

My code is:

function resize() {
    $(".combobox").css('width', '100%');
    var width = $(".combobox").width();
}

I've get the real value of the combobox only after calling the function a second time. The first time is gets the width before executing $(".combobox").css('width', '100%');

I suppose this is fault of the DOM not being recreated after the .css(). Is there any way to force DOM reloading?

Thanks in a开发者_如何转开发dvance!


Try putting the part of the code that gets the width in a timer scheduled for 1 millisecond...

The browser won't do the resize calculation while the script is executing. If you schedule a timer and exit the JS function, the browser will update the state of all the DOM elements that changed, and then launch the timer, so by then the width of your combobox has updated.

I used something very similar in a GWT based webpage

This trick works not only for JS but in many GUI frameworks, which do layout in a delayed way.


Try chaining the two functions:

function resize() {
    var width = $(".combobox").css('width', '100%').width();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号