简体   繁体   中英

Change width and height using Javascript

I am currently working on a simple task, changing the width and height of a div id using javascript. This is my code and I just can't get it to work. Is there something I'm forgetting to add?

HTML:

<!doctype html>
<html>
<head>
</head>
<body>

<div id="box">
    Hello!
</div>

<script src="main.js"</script>
</body>
</html>

Javascript:

function changeSize() {
document.getElementById('box').setAttribute("style","display:block;width:300px;background-color:blue;");
document.getElementById('box').style.width='300px';
}

Issue in refering your script, it does not have a > tag

<script src="main.js"> </script>

DEMO

You are never calling the function itself...

You need to call it after load the script or convert it to anonymous function here is the demo

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