jQuery:尝试获取div高度时的值错误 [英] jquery: wrong values when trying to get div height

查看:417
本文介绍了jQuery:尝试获取div高度时的值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下html结构:

i'm having this html structure:

<div class=container>
    <div class=content>
            Content goes here
    </div>
</div>

我正在使用以下方法读取div.content高度:

i'm reading out the div.content height using:

var height = $("div.content").height();

将返回17(在我的测试用例中)

will return 17 (in my testcase)

效果很好(使用萤火虫将其与实际高度进行比较时). 问题是-当填充连续的文本> 2行时,我得到了错误的值. 2行会给我34(正确),但是3行也会返回34,尽管实际高度是51.当然,当填写大量文本时,结果值都是错误的. 对我来说似乎是一个jquery错误..任何解决方法的想法吗? 谢谢

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

ps:在IE中完美运行,firefox返回了错误的值..

ps: works perfect in IE, firefox returns a wrong value ..

推荐答案

改为使用 $("div.content").outerHeight(); .

Use $("div.content").outerHeight(); instead.

如果要在括号中添加-true-,则要包括所有边距&填充物也是如此. $('div.content').outerHeight(true);

If you add -true- in the parenthesis if you want to include all margins & paddings as well. $('div.content').outerHeight(true);

如果您没有很好的 CSS重置,或者使用的是旧版浏览器(即6/7),则跨浏览器的高度可能会有所不同,但是如果重置良好,它们之间的差异将是非常微小.

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.

jsFiddle演示

这篇关于jQuery:尝试获取div高度时的值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆