#mybox{
position:relative;
left:20px;
top:20px;
}
如上我想通过javascript来改变left和top的值该怎么弄???
2 个解决方案
#1
document.getElementById('mybox').style.left = '0px'
document.getElementById('mybox').style.top = '0px'
#2
<style>
#mybox{
position:relative;
left:20px;
top:20px;
height:20px;
width:40px;
background-color:green
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#mybox").click(function(){
$(this).css({
"left":'60px',
"top":'60px'
});
});
})
</script>
<div id="mybox">
点我改变位置
</div>
#1
document.getElementById('mybox').style.left = '0px'
document.getElementById('mybox').style.top = '0px'
#2
<style>
#mybox{
position:relative;
left:20px;
top:20px;
height:20px;
width:40px;
background-color:green
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#mybox").click(function(){
$(this).css({
"left":'60px',
"top":'60px'
});
});
})
</script>
<div id="mybox">
点我改变位置
</div>