首页 > 代码库 > jQuery选择器绝对定位实现div覆盖
jQuery选择器绝对定位实现div覆盖
新建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="http://www.mamicode.com/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覆盖