这是我的代码:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="a">BELLA</div>
<script type ="text/javascript">
var myDiv = document.getElementById("a");
var myVar = setInterval(spostaDiv, 1000);
function spostaDiv () {
myDiv.style.marginLeft = "+10px";
}
</script>
</body>
</html>
问题是div仅移动一次。为什么它不能连续运行?
答案 0 :(得分:4)
您需要增加边距值才能像这样移动div-
var myDiv = document.getElementById("a");
var myVar = setInterval(spostaDiv, 1000);
var margin = 0;
function spostaDiv() {
margin += 10;
myDiv.style.marginLeft = margin + "px";
}
<div id="a">BELLA</div>