This question already has answers here:
What is difference between width, innerWidth and outerWidth, height, innerHeight and outerHeight in jQuery

(6个答案)


5年前关闭。




根据盒子模型,元素的总宽度为=内容宽度+边距+填充+边框。

如果定义div元素,并添加width = 200,margin = 3px,border = 4px和padding = 1px。然后使用$('div').width()在JS代码中查询时,它将返回200而不是根据公式的宽度。

最佳答案

请检查width();规格。它返回没有边距,填充和边框的宽度。

如果要包括填充和边框,则必须使用.outerWidth();如果也要包括边距,则必须使用.outerWidth(true)

希望这可以帮助

09-30 12:59
查看更多