获取css顶值作为数字不是字符串? [英] Get css top value as number not as string?
本文介绍了获取css顶值作为数字不是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在jQuery中,你可以获得相对于父对象的顶部位置作为数字,但是如果在 px
中设置css顶部值, 。
说我有以下:
In jQuery you can get the top position relative to the parent as a number, but you can not get the css top value as a number if it was set in px
.
Say I have the following:
#elem{
position:relative;
top:10px;
}
<div>
Bla text bla this takes op vertical space....
<div id='elem'>bla</div>
</div>
$('#elem').position().top; //Returns the number (10+(the vertical space took by the text))
$('#elem').css('top'); //Returns the string '10px'
但是我想使用css top属性作为数字 10
。
如何实现这一点?
But I want to have the css top property as the number 10
.
How would one achieve this?
推荐答案
p>您可以使用parseInt()函数将字符串转换为数字,例如:
You can use the parseInt() function to convert the string to a number, e.g:
parseInt($('#elem').css('top'));
更新:
parseInt($('#elem').css('top'), 10);
强制将其解析为十进制数,否则以0开头的字符串可能会被解析为一个八进制数字(可能取决于使用的浏览器)。
Forces it to be parsed as a decimal number, otherwise strings beginning with '0' might be parsed as an octal number (might depend on the browser used).
这篇关于获取css顶值作为数字不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文