oDiv.style.left=100+"px"; //不報錯 // oDiv.style.left=100px; 會報錯?原代碼如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#div{
position: absolute;
width: 100px;
height:100px;
background-color: red;
}
</style>
</head>
<body>
<div id="div">
</div>
</body>
<script>
var oDiv =document.getElementById("div");
div.onmousedown=function(){
oDiv.style.left=100+"px"; //不報錯
// oDiv.style.left=100+"px"; 會報錯
}
</script>
</html>
2017-01-18
給oDiv.style.left賦值必須是字符串才行,100+‘px’會把結果隱式的轉換為字符串類型即'100px',而直接賦值為100px不是字符串 則會報錯。
2017-01-18
px是字符串,應該用引號引起來