2012-05-03 35 views
1

我需要获取表中的位置元素。 但函数position()让我返回其他数据。如何通过元素获得左位置

img - name elemnt。 i = 0;

alert('0 ' + $(img + i).attr('style')); 

var position = $(img + i).position(); 
var left = Math.round(position.left); 

alert('1 left= ' + left); 
alert('1 ' + $(img + i).attr('style')); 
$(img + i).attr('style', 'position:absolute;display:block;left:' + left + 'px;top:' + position.top + 'px;'); 
alert('2 ' + $(img + i).attr('style')); 

迹:

0的位置是:绝对的;显示:块;左边:289px;顶部:0像素;

1左= 582

1的位置是:绝对的;显示:块;左边:289px;顶部:0像素;

2 position:absolute; display:block; left:582px; top:159.0833282470703px;

更新: HTML:

<table cellpadding="0" id="target" cellspacing="0" width="602" height="500" style="float: left; 
     margin: 0px; padding: 0px; position: relative; background: url(/content/games/kamikaze2/back.jpg) no-repeat 0 0;"> 
     <tbody> 
      <tr> 
       <td valign="top"> 
        <img id="stone_0" src="/content/games/kamikaze2/stone0.gif" width="31" height="28" 
         class="c" onclick="go()" > 
</td> 
</tr> 
</tbody> 
</table> 
+0

你能否提供一些标记。 – VisioN

+0

“$(img + i)”?真奇怪。什么是img? –

+0

img - 这个var“stone_”。 – Mediator

回答

0
var position = $(img + i).position(); 
var left = Math.round($(img + i).css("left"));