1

テーブル内の要素を検出しようとしました。ただし、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>
4

2 に答える 2

2

position:relative; を追加してみてください。親要素へ

于 2013-10-11T08:41:07.923 に答える
-1

position はオフセットの親に対する相対位置を返し、offset はドキュメントに対して同じことを行います。明らかに、ドキュメントがオフセットの親である場合 (よくあることですが)、これらは同一になります。

于 2013-10-11T08:37:10.663 に答える