2013-10-11 43 views
1

我试图检测表内的元素。但是,jquery返回相同的值的偏移量和位置。我想要的是元素相对于表格列的位置。jQuery:偏移量和位置返回相同的值

为什么我的jQuery偏移量和位置都返回相同的值?返回的值实际上是偏移值。我如何获得头寸价值?

谢谢。

这里是我的代码:

<!doctype html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>test offset & position</title> 
</head> 
<!-- Bootstrap core CSS --> 
<link href="bootstrap.min.css" rel="stylesheet"> 
<style type="text/css"> 
table.table tbody tr td{ 
    vertical-align:middle; 
} 
</style> 
<body> 
    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-hover table-bordered"> 
    <thead> 
    <tr> 
    <th>Rendering engine</th> 
    <th>Browser</th> 
    <th>Platform(s)</th> 
    <th>Engine version</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
    <td><select name="select" class="detect"> 
    <option>100</option> 
    <option>20</option> 
    <option>3</option> 
    <option>4</option> 
    <option>5</option> 
    </select> 
    <div class="cover"></div></td> 
    <td><p>Internet 
    Explorer 4.0</p> 
    <p>dsfds</p> 
    <p>df</p></td> 
    <td><textarea class="detect">4444 444 

    dsfdsfdsf ds fds f 

    dsf 
    ds 
    fsd 
    fs</textarea></td> 
    <td><div><textarea class="detect">4</textarea></div></td> 
    </tr> 

</tbody> 
</table> 
<script src="jquery-1.10.2.min.js"></script> 

<script> 
$(function(){ 
    $(".detect").focus(function(e) { 
    console.log("height:"+$(this).height()+" width:"+$(this).width()+" position top:"+$(this).position().top+" position left:"+$(this).position().left +" offset top:"+$(this).offset().top+" offset left:"+$(this).offset().left); 
    }); 
}); 
</script> 
</body> 
</html> 
+0

请发布您的HTML和jQuery代码。 –

+0

我已经用我的代码编辑帖子。 – user1995781

+0

'offset()'和'position()'为我返回不同的值:http://jsfiddle.net/RoryMcCrossan/s9ucs/ –

回答

-1

位置返回相对于抵消母公司的位置,和偏移是否相对于文档相同。显然,如果文档是偏移父项,通常情况下,这些将是相同的。

2

尝试添加position:relative;到父元素

相关问题