简体   繁体   中英

Can't obtain div style width or height

I don't know why but I can't set the canvas width and height, and am not sure what I am doing wrong.

I create the page like this:

//create div
var d = document.createElement('div'); 
d.id = 'G';
d.className = 'GameOuput';  
document.getElementById('E').appendChild(d);    

var i = 1;
var c = document.createElement('canvas');
    c.id = 'layer' + i;
    c.className = 'c';
    c.style['z-index'] = i;

    document.getElementById('G').appendChild(c);  

    console.log(document.getElementById('G').style.width);  
    c.width = document.getElementById('G').style.width;
    c.height = document.getElementById('G').style.height; 

The the canvas shows:

<canvas id="layer1" class="c" style="z-index: 1;" width="0" height="0"></canvas>

console.log(document.getElementById('G').style.width); = blank

What am i doing wrong here??

Try:

document.getElementById('G').offsetWidth;

Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present, if rendered) and the element CSS width.

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