简体   繁体   中英

Dynamic width of DIV (offsetWidth issue)

I face issue with retrieving (via javascript) width of the div which content is changed (just before reading the widht via offsetWidth) in dynamic way (via changing innerHTML or using createTextNode).

Here is some sample code:

var con = document.getElementById('avContent'); //content div within page

var temp = document.createElement('div');<br /> 
var text1 = document.createTextNode('CCCCC');<br /> 
temp.appendChild(text1);<br /> 
con.appendChild(temp);<br /> 
var length1 = temp.offsetWidth;<br /> 
var text2 = document.createTextNode('CCCCC33333333vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv');<br /> 
temp.removeChild(text1);<br /> 
temp.appendChild(text2);<br /> 
con.removeChild(temp);<br /> 
con.appendChild(temp);
var length2 = temp.offsetWidth;<br /> 

The length1 and length2 do have the same width.. (the same result I get while using innerHTML instead of createTextNode).

Looks like it's the same issue like described in following discussion:
http://www.webdeveloper.com/forum/showthread.php?t=187716

Does anybody have answer (work around)?
Thanks much for help in advance.

Inherit CSS继承-外部元素(另一个div或Table)中DIV的宽度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM