简体   繁体   中英

How to apply CSS only to numbers in paragraph? (without <span>)

How to apply CSS only to numbers in paragraphs without wrapping numbers in span s?

<p>Text and numbers: 123</p>
<p>123 and text</p>

Are there any selectors? In jQuery, maybe. Or somehow parse it using javascript...

Expected result:


在此输入图像描述

You can't do this with only CSS. Since you also marked your question with a jQuery tag, you can however do it this way:

$('p').html(function(index, value) {
    return value.replace(/(\d+)/g, '<span>$1</span>');
});

jsFiddle example

There is no way to do what you want without wrapping the desired text strings in elements, like spans or anything else.

With jQuery:

(function($) {
    $(function() {
        $('p').each(function() {
            var el = $(this),
                html = el.html();

            html = html.replace(/(\d)/gi, "<span class='numbers'>$1</span>");

            el.html(html);

        });
    });
})(jQuery);

try something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM