获取物体相对于父级的偏移量,也就是top值和left值,而这个只对可见物体有用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#box{
position: absolute;top: 100px;left: 100px;width: 200px;height: 200px;
border: 1px solid red;
}
p{
position: absolute;top: 100px;left: 100px;
}
</style>
</head>
<body>
<div id="box">
<p>This is a paragraph.</p>
<button>获得 p 元素的位置坐标</button>
</div>
</body>
</html>
<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
<script type="text/javascript">
$("button").click(function(){
x=$("p").position();
console.log("positionLeft:" + x.left + "positionTop:" + x.top);
});
</script>
结果如下: