jQuery 获取没有'px'的元素的行高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10507296/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:26:28  来源:igfitidea点击:

Get line-height of element without 'px'

jquery

提问by cusejuice

How do I retreive the line-height of an element without the "px"?

如何在没有“px”的情况下检索元素的行高?

Using this I get the full line-height value including the px.

使用这个我得到包括像素在内的完整行高值。

$('#example').css('line-height');

回答by Kyle Macey

Parse it as an integer with parseInt

将其解析为整数 parseInt

parseInt($('#example').css('line-height'), 10);

parseInt($('#example').css('line-height'), 10);

Outputs:

输出:

18

18

As an integer. The other solutions maintain the String type.

作为整数。其他解决方案维护 String 类型。

EDIT

编辑

For values that may contain decimal points, you can use parseFloat()

对于可能包含小数点的值,您可以使用 parseFloat()

parseFloat($('#example').css('line-height'));

parseFloat($('#example').css('line-height'));

Outputs:

输出:

18.4

18.4

回答by Selvakumar Arumugam

Just replace the pxwith ''.

只需更换px''

$('#example').css('line-height').replace('px', '');

$('#example').css('line-height').replace('px', '');

回答by Jorge

save it into a variable an then make a replace

将其保存到变量中,然后进行替换

var aux = $('#example').css('line-height').replace('px', '');

回答by htatche

In coffeescript

在咖啡脚本中

getElementProperty = (el, property) -> 
  val = el.css(property).replace "px" , "" 

  parseInt val


getElementProperty $("#myElement"), "line-height"

This should to it !

这个应该吧!