简体   繁体   中英

next text wrap and add text

I do not know how to put text into strong and add "x".

<span>something</span> 5 
<span>something</span> 4

And I want:

<br /><span>something</span> <strong>5x</strong>
<br /><span>something</span> <strong>4x</strong>

My solution:

$("span").each(function() {
    $(this).before("<br />");
    $(this.nextSibling).wrapAll("<strong></strong>").after("x");    
});

But the output is not perfect:

<br /><span>something</span> <strnong>5 x</strong>
<br /><span>something</span> <strnong>4 x</strong>

How to remove blank between 5 and x?

Try

$("span").each(function () {
    $(this).before("<br />");
    $(this.nextSibling).wrapAll("<strong></strong>").parent().text(function (i, t) {
        return t.replace(/\s+$/, '') + 'x'
    });
});

Demo: Fiddle

您可以使用$('strong').each()选择每个strong元素,然后使用$(this).replace(/ /g,'')去除空格。

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