2013-10-11 40 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;到父元素

相關問題