jquery函数宽度()总是返回0。这是我的布局
<div class="entity" id="id-@user.id">
<div class="inactive">
<div class="touch-header">
<div>
<small>@user.name<br>@user.surname</small>
</div>
<div role="button" data-toggle="modal" id="home_office_id-@user.id" class="home_office_log pull-right" style="margin-top: -2.2%;text-align:center; margin-right: 2px;opacity: 0.8;width: 50px; height: 50px;position: absolute; margin-left: 4.9%;z-index: 1;">
...
</div>
</div>
</div>
</div>
然后,我有javascript代码,我希望通过动画方法将标记< small> < /small>的内容移动到div class=“实体”的中间。
var lab = $('div.entity#id-' + userId).children().
children().children().children('small');
lab.css('position', 'relative').animate({
left: $('.entity#id-'+userId).width() - lab.width() / 2 - 15
}, 500);
但在这两种情况下,函数width()都返回0。我能做什么?
发布于 2013-07-29 00:17:24
问题解决了,这一切都是以克隆的形式生成另一个页面到不同的选项卡,所以即使布局只生成一次,jquery对象也被两次创建到DOM中。该选择器返回两个对象,但其中只有一个正确地设置了宽度属性。无论如何,谢谢你的回复,特别是@Juan
发布于 2013-07-26 11:11:30
尝试innerWidth
或outerWidth
,它们实际上计算值,width
AFAIK只返回CSS中的值。
https://stackoverflow.com/questions/17889006
复制相似问题