Home >Web Front-end >HTML Tutorial >Why can't I get the width and height_html/css_WEB-ITnose here?
www.style.width=document.body.offsetWidth;
www.style.height=document.body.offsetHeight;
Breakpoint the two lines above
document.body.offsetWidth
document.body.offsetHeight
has certain values
but cannot be assigned to width and height
<style>#www{ background-color:#0F0;}</style><script>function fun(){ var www=document.getElementById('www') ; www.style.width=document.body.offsetWidth; www.style.height=document.body.offsetHeight; www.style.opacity=0.5;}</script></head><body><div id="www" onclick="fun()">哇哈哈</div>
Just change your JS function to the following:
function fun(){ var www = document.getElementById('www'); www.style.width = document.body.offsetWidth+'px'; www.style.height = document.body.offsetHeight+'px'; www.style.opacity = 0.5;}
Just change your JS function to the following:
JavaScript code?12345678function fun(){ var www = document.getElementById('www'); www.style.width = document.body.offsetWidth 'px'; www.style.height = document.……
It is true that px has been added less. Let me correct you here. There is only one div element in the document, so the width and height of document.body are the same as the width and height of the div. Using document.body.offsetWidth is still the same as div.style.width. wide, so the effect cannot be seen