1. 程式人生 > >jQuery選擇器絕對定位實現div覆蓋

jQuery選擇器絕對定位實現div覆蓋

rip jquer 絕對定位 button min 覆蓋 str fun 選擇

新建html文件可以運行(jsp文件下不可運行)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: auto;
}
.d1{
width: 500px;
height: 500px;
background-color: blue;
}
.d2,.d3,.d4{
display: inline;
}
.d2{
background-color: #ccc;
}
.d3{
background-color: #FF0;
}
.d4{
background-color: red;
}
</style>
</head>
<body>
<div class="d1">
<div class="d2">div1</div><br />
<div class="d3">div2</div>
</div>
<div class="d4">div4</div><br />
<input class="btn" type="button" value="ok" />
</body>
<script src="http://code.jquery.com/jquery-1.8.0.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
var d2L=document.querySelector(".d2").offsetLeft;
var d2T=document.querySelector(".d2").offsetTop;
// alert(d2L+"-"+d2T);
$(".btn").click(function(){
$(".d4").css({
"position":"absolute",
"left":d2L,
"top":d2T
})
});
})
</script>
</html>

jQuery選擇器絕對定位實現div覆蓋