[英]Get CSS value in JQuery
I'm using JQuery to try and get the value of the top
from css. 我正在使用JQuery尝试从CSS获取
top
的值。
However, where I have read a lot of things saying $('modal').css('top')
will return a string "40" it is in fact retruning "40px". 但是,在我读过很多东西的地方,
$('modal').css('top')
会返回字符串“ 40”,实际上它在返回“ 40px”。
Is there any way I can get this into an integer format. 有什么办法可以将其转换为整数格式。 The component is not yet rendered on the page, I am trying to find the default position so I can set it using JS.
该组件尚未在页面上呈现,我正在尝试查找默认位置,以便可以使用JS进行设置。 This means that position().Top = 0 etc.
这意味着position()。Top = 0等等。
You can use $.fn.position()
to do that... 您可以使用
$.fn.position()
来做到这一点...
$('modal').position().top
If that doesn't work in your situation, you could use parseInt()
or parseFloat()
, because some browsers will give you fractional portions of pixels in some situations. 如果这在您的情况下不起作用,则可以使用
parseInt()
或parseFloat()
,因为在某些情况下某些浏览器会为您提供像素的分数部分。
One solution involves regex replacement and cast to Number: 一种解决方案涉及到正则表达式替换并强制转换为Number:
var topInt = Number($('modal').css('top').replace(/px$/,''));
http://jsfiddle.net/6n8xu0gf/1/ http://jsfiddle.net/6n8xu0gf/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.