简体   繁体   English

使用 Javascript 更改宽度和高度

[英]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.我目前正在处理一个简单的任务,使用 javascript 更改 div id 的宽度和高度。 This is my code and I just can't get it to work.这是我的代码,我无法让它工作。 Is there something I'm forgetting to add?有什么我忘记添加的吗?

HTML: HTML:

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

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

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

Javascript: 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您需要在加载脚本后调用它或将其转换为匿名函数,这里是演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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