Home >Web Front-end >HTML Tutorial >Why can't I get the width and height_html/css_WEB-ITnose here?

Why can't I get the width and height_html/css_WEB-ITnose here?

WBOY
WBOYOriginal
2016-06-24 12:19:31906browse

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>


Ask for advice


Reply to the discussion (solution)

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn