求教這個錯在哪?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js動畫制作</title>
<style type="text/css">
#div1{
width:200px;
height:200px;
border:2px solid red;
position:absolute;
left:0px;
top:50px;?
? ? }
</style>
<script type="text/javascript">
function startqwe(speed){
var qwe=document.getElementById('div1');
var speed=document.getElementById('txt1').value;
setInterval(function(){qwe.style.left=qwe.offsetLeft+speed+'px';},1000);
</script>
</head>
<body>
<div id="div1"></div>
設置速度<input type="text" id="txt1" />px/s
<input type="button" value="點擊開始運動" style="position:relative;" onclick="startqwe(speed)" />
</body>
</html>
想設計一個自定義速度的運動結果沒有反應麻煩大神看下謝謝
2017-03-31
首先獲取元素要在文檔加載完成之后才能獲取到的,其次是你那個速度是網頁加載完成的時候輸入的,所以要在點擊事件里面獲取,才能得到輸入的速度值,這個值是個字符串用parseInt()轉義下,speed是全局變量可以在函數里面使用,不用帶進去
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js動畫制作</title>
<style type="text/css">
#div1{
width:200px;
height:200px;
border:2px solid red;
position:absolute;
left:0px;
top:50px;?
? ? }
</style>
</head>
<body>
<div id="div1"></div>
設置速度<input type="text" id="txt1"/>px/s
<input type="button" value="點擊開始運動" style="position:relative;" onclick="startqwe()" />
<script type="text/javascript">
//獲取元素要在文檔加載完成之后在獲取,一般寫在最后面
var qwe=document.getElementById('div1');
var speed=document.getElementById('txt1').value;
function startqwe(){
//點擊之后獲取到速度值
speed=document.getElementById('txt1').value;
setInterval(function(){qwe.style.left=qwe.offsetLeft+parseInt(speed)+'px';
},1000);
}
</script>
</body>
</html>
2017-03-31
贊同上面的說法