jquery width得到0

时间:2013-07-26 19:01:54

标签: javascript jquery width

jquery函数width()总是返回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代码,我想移动标签的内容&lt;小&GT; &LT; /小&GT;通过animate方法到div class =“entity”

的中间
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.我该怎么办?

2 个答案:

答案 0 :(得分:0)

问题解决了,所有关于生成另一个页面作为克隆到不同的选项卡,所以即使布局只是一次,jquery对象被创建两次到DOM。并且该选择器返回了2个对象,但只有其中一个正确设置了宽度属性。无论如何,特别感谢@Juan

的回应

答案 1 :(得分:-2)

尝试innerWidthouterWidth,他们实际计算了值width AFAIK只返回CSS中的一个。