猿问

JS动态改变盒子位置,如图

请问点击按钮(往右移动300px)让盒子右移300px在JS里应该怎么写!

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

<style>

*{

margin: 0;

padding: 0;

}

#box{

width: 400px;

height: 400px;

border: 1px solid red;

}

#div1{

width: 200px;

height: 200px;

background-color: #37A3EB;

margin-left:0;

}

</style>

</head>


<body>

<div id="box">

<div id="div1"></div>

</div>

<input type="button" value="往右移动300px" id="it6" />

<script>

window.onload=function(){

var div1=document.getElementById("div1");

var it6=document.getElementById("it6");

it6.onclick=function(){

div1.style.margin="margin-left:300px"

}

}

</script>

</body>

</html>


睡猫喵
浏览 3831回答 3
3回答

stone310

div1.style.marginLeft="300px"
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答