如何使用 jQuery 仅获取 CSS 属性的数字部分? [英] How to get just numeric part of CSS property with jQuery?

查看:31
本文介绍了如何使用 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?

推荐答案

这将从字符串中清除所有非数字、非点和非减号:

This will clean up all non-digits, non-dots, and not-minus-sign from the string:

$(this).css('marginBottom').replace(/[^-d.]/g, '');

负值更新

这篇关于如何使用 jQuery 仅获取 CSS 属性的数字部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆