【问题标题】:Get html element rendered height with jQuery使用 jQuery 获取 html 元素呈现的高度
【发布时间】:2011-10-10 11:00:14
【问题描述】:

一个非常简单的问题。

例如,我有一个这样的类:

.someclass {
  font-size: 1.2em /* let's say that 1em is set to 10px in this example */
  padding: 1em 0 2em 0;
  border: 0.1em solid #000;
}

还有一些简单的html来使用它:

<div class="someclass">Test</div>

我想得到这个元素的生成高度 - 计算出的带有边框和填充的高度。在这个例子中使用 jQuery .height() 方法给出了 line-height 属性的当前值 - 正如预期的那样,因为 div 中的文本只有一行。

我知道我可以自己进行计算,因为它们相对简单,但我想问一下 - jQuery 中有没有内置的解决方案?

【问题讨论】:

  • 我接受了你的,因为它的整体价值;)

标签: jquery css


【解决方案1】:

jQuery 有几种方法可以做到这一点。见http://api.jquery.com/category/dimensions/

在这种情况下,听起来你想要outerHeight()http://api.jquery.com/outerHeight/

【讨论】:

    【解决方案2】:

    innerHeight

    此方法返回元素的高度,包括顶部和底部填充,以像素为单位。

    【讨论】:

    • 手动添加 (borderwidth*2) 的宽度和高度(如果是 px 则)
    【解决方案3】:

    使用.outerHeight()

    http://api.jquery.com/outerHeight/

    获取集合中第一个元素的当前计算高度 匹配的元素,包括内边距、边框和可选的边距。

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 2012-05-01
        • 2014-03-20
        • 2014-09-14
        • 2011-08-05
        • 2013-12-28
        • 2017-01-21
        • 1970-01-01
        • 2010-10-06
        相关资源
        最近更新 更多