How to apply css to only numbers in a text inside/or any

element?

前端 未结 5 963
日久生厌
日久生厌 2020-12-06 02:39

I am Using a Regional language unicode font-face in my site but the numbers are not looking good.

So I want to apply new font-style or css to numbers only..

相关标签:
5条回答
  • 2020-12-06 03:11

    Better with this

    $('p').html(function(i, v){
        return v.replace(/(\d+)/g, '<span class="number">$1</span>');
    });
    

    With + you avoid one span per complete number (span for 321), not one per each number found (span for 3 for 2 and for 1)

    0 讨论(0)
  • 2020-12-06 03:16

    There is no way to apply CSS to all numbers specifically. In each number tag you could add the attribute class='number' and then in the CSS you could add

    .number {
       font-family: arial;
    }
    
    0 讨论(0)
  • 2020-12-06 03:21

    You can use the regex replace and detect the numbers then add the class

    following code:

    $('p').html(function(i,c) {
        return c.replace(/\d+/g, function(v){
        return "<span class='numbers'>" + v + "</span>";
        });
    });
    .numbers
    {
    color:red;
    font-size:30px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <p>
    View 11 new out of 11 message(s) in your inbox
    </p>

    0 讨论(0)
  • 2020-12-06 03:29

    You can wrap all numbers in p tags with a <span class="number">:

    CSS

    .number {
       font-family: Verdana;
    }
    

    jQuery

    $('p').html(function(i, v){
        return v.replace(/(\d)/g, '<span class="number">$1</span>');
    });
    

    But personally, I would go with James suggestion ;)

    http://jsfiddle.net/ZzBN9/

    0 讨论(0)
  • 2020-12-06 03:35

    This can be done using CSS's unicode-range property which exists within @font-face.

    The numbers 0 to 9 exist in Unicode within the range U+0030 to U+0039. So what you'll need to do is include a font alongside your existing font which specifically targets this range:

    @font-face { 
        font-family: 'My Pre-Existing Font';
        ...
    }
    @font-face {
        font-family: 'My New Font Which Handles Numbers Correctly';
        ...
        unicode-range: U+30-39;
    }
    

    The result of this will be that every instance of Unicode characters U+0030 (0) through to U+0039 (9) will be displayed in the font which specifically targets that range, and every other character will be in your current font.

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