简体   繁体   中英

How get left position by element

I need get position element in table. But function position() returns me to the other data.

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'));

trace:

0 position:absolute;display:block;left:289px;top:0px;

1 left= 582

1 position:absolute;display:block;left:289px;top:0px;

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

update: 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>
var position = $(img + i).position();
var left = Math.round($(img + i).css("left"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM