我想得到margin-top大小和height另一层的总和。
的边距顶部.nav1为 30px,高度.main为 28px
我使用这段代码:
$('.nav').css('margin-top').replace('px', '') + $('.main').outerHeight()
但我总和的结果是3028
如何计算这两个数字的总和?
outerHeight(true)如果您想计算margin-top和margin-bottom属性,您可以使用在没有数学的情况下将边距添加到高度:http: //api.jquery.com/outerheight/#outerHeight-includeMargin
var heightWithMargin = $('.main').outerHeight(true);
这是因为您要附加两个字符串,而不是添加两个整数。试试这个:
var total = parseInt($('.nav').css('margin-top'), 10) + $('.main').outerHeight();