Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery元素宽度计算不正确_Javascript_Jquery_Html_Css_Browser - Fatal编程技术网

Javascript jQuery元素宽度计算不正确

Javascript jQuery元素宽度计算不正确,javascript,jquery,html,css,browser,Javascript,Jquery,Html,Css,Browser,具有简单的html代码 <div id="header"> <div id="headerBox"> <div id="headerText"> </div> </div> </div> 和jQuery(2.x edge) 函数调整大小(win){ 变量大小; var w=$('#headerBox').width(); $('#headerText').html(''); $('#headerBo

具有简单的html代码

<div id="header">
  <div id="headerBox">
    <div id="headerText">
    </div>
  </div>
</div>
jQuery(2.x edge)

函数调整大小(win){
变量大小;
var w=$('#headerBox').width();
$('#headerText').html('');
$('#headerBox').css('font-size',1+'px');
$('#headerText').html(
“灯头盒元件的宽度为”+w+“px”);

而($('#headerText').width()JQuery
.width
不包括#headerbox的填充。请使用
.outerWidth
获得正确的宽度

var w = $('#headerBox').outerWidth();
发现问题: 由于
填充:1em;
用于磁头盒,此元素的宽度随
字体大小的变化而变化

因此,在
while
循环中,我需要处理最新的信息,而不是一开始存储的信息

所以

var w = $('#headerBox').width();
...
while ($('#headerText').width() <= w) {
  ...
}
var w=$('#headerBox').width();
...

while($('#headerText').width()也许您想要外套宽度()相反,JSFIDLE在Firefox中为我正确显示。@A.Wolff-我使用的是Chrome 31.0.1650.63m,但它没有正确显示。Trange,我使用的是相同的Chromeversion@A.Wolff-关闭它,再次打开,然后在小提琴中调整结果窗口的大小-你看不出有什么不同吗?-1::这不是我问题的答案。当然
.width()
不包括填充。我知道。我不希望在计算中包含填充。
var w = $('#headerBox').outerWidth();
var w = $('#headerBox').width();
...
while ($('#headerText').width() <= w) {
  ...
}
while ($('#headerText').width() <= $('#headerBox').width()) {
  ...
}