Vertical Text with jQuery

后端 未结 6 1822
猫巷女王i
猫巷女王i 2021-02-15 14:11

I\'m looking to vertically align text by adding
tags between characters with jQuery.

相关标签:
6条回答
  • 2021-02-15 14:50

    document.write("vertical text".split("").join("<br/>"));

    Edit: Hole in one!

    0 讨论(0)
  • 2021-02-15 14:57

    This builds on Sebastian H's answer, but I tested it and this works

        var element = $( '#foo label' );
        var newData = '';
        var data = element.text();
        var length = data.length;
        var i = 0;
        $( '#foo label' ).html("");
        while( i < length )
        {
                $( '#foo label' ).append(data.charAt( i ) + "<br />")
                i++;
        }
    
    0 讨论(0)
  • 2021-02-15 14:58

    Let's go golfing!

    $('#foo label').html($('#foo label').text().replace(/(.)/g,"$1<br />"));
    

    Completely untested, but the pattern in the regex looks like a boob.

    0 讨论(0)
  • 2021-02-15 15:10

    Not tested, but it should work.

    var element = $( '#foo label' );
    var newData = '';
    var data = element.text();
    var length = data.length;
    var i = 0;
    
    while( i < length )
    {
    
        newData += data.charAt( i ) + '<br />';
        i++;
    
    }
    
    element.html( newData );
    
    0 讨论(0)
  • 2021-02-15 15:12

    Mr Kurt's answer works well for a single id, but if you want something more useful that can be applied to several elements try something like this:

    $.each( $(".verticalText"), function () { $(this).html($(this).text().replace(/(.)/g, "$1<br />")) } );

    Then just set class="verticalText" on the elements you want to be formatted like this.

    And as a bonus it keeps the boob regex.

    0 讨论(0)
  • 2021-02-15 15:12

    Why use a while loop when you can use jQuery's builtin each method?

    $.each( $('#foo').text(), function(){ $('#foo').append(this + '
    '); } );

    There. It works. You can test it.

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