如何使用jQuery只获取CSS属性的数字部分? [英] How to get just numeric part of CSS property with jQuery?
本文介绍了如何使用jQuery只获取CSS属性的数字部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要根据CSS属性进行数值计算。但是,当我使用这个来获取信息:
I need to do a numeric calculation based on CSS properties. However, when I use this to get info:
$(this).css('marginBottom')
它返回值'10px'。无论是 px
还是%
或 em
或任何?
it returns the value '10px'. Is there a trick to just getting the number part of the value no matter whether it is px
or %
or em
or whatever?
推荐答案
这将清除所有非数字, -dot和非减号:
This will clean up all non-digits, non-dots, and not-minus-sign from the string:
$(this).css('marginBottom').replace(/[^-\d\.]/g, '');
UPDATED负值
这篇关于如何使用jQuery只获取CSS属性的数字部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文