嗨,我想我想在編輯我的div「盒子」的大小(寬度和高度)時錯過了一些至關重要的東西。調整高度和寬度風格;像素值出現爲字符串
當我想查看「box」div的高度或寬度的值時,我可以使用getElementById
並查找寬度或高度,並返回像素值。
document.getElementById("box").style.height ">> 200px"
不錯。
但是,如果我想編輯該DIV中像素的數量,它看起來像200px正在返回一個字符串而不是一個數字,所以我不能添加或減去像素。
`document.getElementById("box").style.height += 200
>>"200px200"
document.getElementById("box").style.height += 200px
>>VM1143:1 Uncaught SyntaxError: Invalid or unexpected token
document.getElementById("box").style.height += "200px"
">>200px200px"`
我錯過了什麼,我無法編輯此div的寬度和高度?添加和減去像素時是否需要刪除「px」?
謝謝,下面是我使用的代碼。
<!DOCTYPE html>
<html>
<head>
<title>
Day 12 - Functions 2 Computation
</title>
<style>
#box {
width: 200px;
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div id="group">
<button id="bigger">Bigger</button>
<button id="smaller">Smaller</button>
<hr />
<button id="blue">Blue</button>
<button id="red">Red</button>
<button id="green">Green</button>
<div id="status"></div>
</div>
<div id="box" style="height: 200px; width: 200px"></div> <-- This one is giving me issues
<script type="text/javascript"> </script>
</body>
</html>
感謝這最終解決了我的問題。我試圖找出一種方法,不必在另一行添加「px」,但我想這是不能避免的。 – Chef1075