1

私はセンチス/H1を持っていますが、これを分割して以下のようにする必要があります

元の状態

<h1>This is the heading </h1>

<h1>This <br> 
is <br> 
the <br> 
heading </h1>
4

4 に答える 4

3

私はお勧めします:

$('h1').each(
    function(){
        var that = $(this),
            newText = that.text().replace(/\W/g,'<br />');
        that.html(newText);
    });

参考文献:

于 2012-05-13T15:14:44.470 に答える
2
$("h1").html( $("h1").text().replace(/ /g,"<br>"))​
于 2012-05-13T15:17:56.240 に答える
1
$('h1').each(function() {
    var txt = $(this).html().replace(/^\s*(.*?)\s*$/,'$1');
    // this trims the string
    $(this).html(txt.split(/\s+/).join(' <br/>')+' ');
    // this splits the character groups (not containing spaces)
    // and joins them by a br tag, then adds an extra space at the end.
});

代替、および短いバージョン ( VisioNに感謝) :

$('h1').each(function() {
    $(this).html($.trim($(this).html()).split(/\s+/).join(' <br/>')+' ');
});
于 2012-05-13T15:24:34.870 に答える
0

これを試してください:

var h = $('h1').text().split(' ').join('<br>'); 
$('h1').html(h);
于 2012-05-13T15:25:03.827 に答える