Uppercase first letter of variable

后端 未结 23 1449
粉色の甜心
粉色の甜心 2020-11-30 20:55

I have searched over the web can can\'t find anything to help me. I want to make the first letter of each word upper case within a variable.

So far i have tried:

23条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 21:00

    Ever heard of substr() ?

    For a starter :

    $("#test").text($("#test").text().substr(0,1).toUpperCase()+$("#test").text().substr(1,$("#test").text().length));
    


    [Update:]

    Thanks to @FelixKling for the tip:

    $("#test").text(function(i, text) {
        return text.substr(0,1).toUpperCase() + text.substr(1);
    });
    

提交回复
热议问题