jquery:尝试获取 div 高度时出现错误的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12184133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery: wrong values when trying to get div height
提问by Fuxi
i'm having this html structure:
我有这个 html 结构:
<div class=container>
<div class=content>
Content goes here
</div>
</div>
i'm reading out the div.content height using:
我正在使用以下方法读出 div.content 高度:
var height = $("div.content").height();
will return 17 (in my testcase)
将返回 17(在我的测试用例中)
works nice so far (when comparing it with the actual height using firebug). the problem is - when filling up continuous text >2 lines i'm getting a wrong value. 2 lines will give me 34 (correct) but 3 lines will also return 34 although the actual height is 51. of course, when filling in plenty of text the resulting value is all wrong. seems like a jquery bug to me .. any ideas for a workaround? thanks
到目前为止效果很好(使用萤火虫将其与实际高度进行比较时)。问题是 - 当填充连续文本 > 2 行时,我得到了错误的值。2 行会给我 34(正确)但 3 行也会返回 34,尽管实际高度是 51。当然,当填写大量文本时,结果值全是错误的。对我来说似乎是一个 jquery 错误……有什么解决方法的想法吗?谢谢
ps: works perfect in IE, firefox returns a wrong value ..
ps:在 IE 中完美运行,firefox 返回错误值..
回答by Mark Pieszak - Trilon.io
Use $("div.content").outerHeight();
instead.
使用$("div.content").outerHeight();
来代替。
If you add -true- in the parenthesis if you want to include all margins & paddings as well.
$('div.content').outerHeight(true);
如果您还想包括所有边距和填充,请在括号中添加 -true-。
$('div.content').outerHeight(true);
Also cross-browser heights may differ if you don't have very good CSS Resets, or are looking at a legacy browser (ie6/7), but with a good reset their differences will be very miniscule.
如果您没有很好的CSS 重置,或者正在查看旧版浏览器(ie6/7),跨浏览器的高度也可能会有所不同,但如果重置良好,它们的差异将非常小。