How to get just numeric part of CSS property with jQuery?

前端 未结 15 1666
悲哀的现实
悲哀的现实 2020-11-28 18:53

I need to do a numeric calculation based on CSS properties. However, when I use this to get info:

$(this).css(\'marginBottom\')

it returns

相关标签:
15条回答
  • 2020-11-28 19:39

    Id go for:

    Math.abs(parseFloat($(this).css("property")));
    
    0 讨论(0)
  • 2020-11-28 19:41

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

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

    UPDATED for negative values

    0 讨论(0)
  • 2020-11-28 19:43
    $(this).css('marginBottom').replace('px','')
    
    0 讨论(0)
  • 2020-11-28 19:43

    use

    $(this).cssUnit('marginBottom');
    

    which return an array. first index returns margin bottom's value(example 20 for 20px) and second index returns margin bottom's unit(example px for 20px)

    0 讨论(0)
  • 2020-11-28 19:44

    If it's just for "px" you can also use:

    $(this).css('marginBottom').slice(0, -2);
    
    0 讨论(0)
  • 2020-11-28 19:48

    The simplest way to get the element width without units is :

    target.width()
    

    Source : https://api.jquery.com/width/#width2

    0 讨论(0)
提交回复
热议问题