【发布时间】:2013-07-29 16:17:24
【问题描述】:
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 代码,我想通过 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。我该怎么办?
【问题讨论】:
-
.children().children().children().children('small');--.find("small") -
你必须显示生成的 HTML,我假设
@user-id被替换,因为 jQuery 告诉我Uncaught Error: Syntax error, unrecognized expression: div.entity#id-@user.id -
@JuanMendes - 这可能是 asp.net MVC 剃须刀代码
-
是的,它是从java框架生成的。只要放任何数字,它不会改变任何东西。并为变量 userId 使用相同的数字
-
@ParvSharma 我不在乎它是什么服务器端框架。为了清楚问题,您应该显示浏览器看到的实际 HTML。同时告诉我们
userId是什么。你提出的问题越好,就越有可能有人会提供帮助,如果我们必须继续挖掘,我们就会停止寻找,就像我刚才所做的那样
标签: javascript jquery width