【问题标题】:How get left position by element如何按元素获取左侧位置
【发布时间】:2012-05-03 10:43:39
【问题描述】:

我需要在表格中获取位置元素。 但是函数 position() 将我返回到其他数据。

img - 名称元素。 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;顶部:0px;

还剩 1 个 = 582

1 个位置:绝对;显示:块;左:289px;顶部:0px;

2 位置:绝对;显示:块;左:582px;顶部: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>

【问题讨论】:

  • 你能提供一些标记吗?
  • "$(img + i)" ?那很奇怪。 img 是什么?
  • img - 这个变量“stone_”。
  • 使用 $('#'+img+i) 代替 $(img + i)
  • 我错了,'#stone_ - 屏幕上发生了动作,但没有有效数据。

标签: javascript jquery


【解决方案1】:
var position = $(img + i).position();
var left = Math.round($(img + i).css("left"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 2013-08-11
    • 2019-03-21
    • 1970-01-01
    • 2011-10-23
    • 2016-03-21
    • 2014-10-26
    • 2014-10-05
    相关资源
    最近更新 更多