Replace All (.) Periods in Paragraph

I am trying to replace all periods with a degree symbol. Using each method replaces only the first period within the paragraph tag, but not all of them. I also try to ensure that the periods inside the anchor tags are ignored and not replaced, so the links will not be broken by my code. Can anyone help?

$('div.post').each(function(){
    var $h = $(this);
    $h.html( $h.html().replace( '.', '<span class="period">&deg;<\/span>' ) );
});

Edit: I should have mentioned, I want to keep the "period" class, which makes it complex.

+3
source share
2 answers
var span = $('<span class="period">°</span>').get(0);

$('p, p *').each(function (index,element) {
    $.each(element.childNodes, function (index,node) {
        if (node.nodeType == 3) {
            $.each(node.nodeValue.split('.'), function (index,fragment) {
                if ( index > 0 ) {
                    element.insertBefore(span.cloneNode(true),node);
                }
                element.insertBefore(document.createTextNode(fragment),node);
            });
            element.removeChild(node);
        }
    });
});

. , node "." , node, span, .

+1

g () , :

$h.html( $h.html().replace(/\./g, '<span class="period">&deg;<\/span>' ) );

, HTML-. , , - , HTML , , .

+1

Source: https://habr.com/ru/post/1726501/


All Articles