0
有了这个代码,我试图中心的项目因宽度:
var name_ = 'Random name';
$('.arrow .text').text(name_);
$('.arrow .text').css({'marginLeft':'-'+$('.arrow .text').width()/2+'px'});
console.log($('.arrow .text').length +' '+ $('.arrow .text').width());
HTML
<div style="float:left;">
<div class="map">
<div class="arrow">
<img src="http://www.discoverdartmouth.com/images/map.point.png" />
<div class="text"></div>
</div>
<img src="http://www.freepik.es/foto-gratis/mapa-del-mundo_17-903095345.jpg" id="img1" />
<br/>
<br/>
<span class="spain mPointer">Spain</span> <span class="australia mPointer">Australia</span>
</div>
</div>
线路设置保证金不工作和控制台日志输出"1 0"
,因此该项目存在但没有宽度。
我在这里错过了什么?
请问您可以发布您的HTML。这听起来像是应该使用HTML/CSS而不是JS/jQuery来实现的。 –
不要使用jQuery来居中HTML元素。这就是CSS的用途。 – Blender
您不需要添加'px'。 – SLaks