javascript capitalization

假装没事ソ 提交于 2019-12-13 04:42:46

问题


var font_name = $(".font").val();

I have this in my JavaScript code. In my html I have an input form with the class .font.

I want to capitalize the first letter of each word in .font, so for example if someone types in Lucida sans it'll turn into Lucida Sans. I couldn't find a jQuery method that does it for you so I guess I have to use actual JavaScript but I really have no idea how.

var font_first = font_name.substr(0,1);
var font = font_first.toUpperCase() + font_name.substr(1, font_name.length - 1);

I used this to capitalize the first letter of the whole font name but as I said I need to capitalize the first letter of each word.


回答1:


Can you not just use CSS?

.font {
    text-transform: capitalize;
}

In jQuery:

$(".font").css("text-transform","capitalize");



回答2:


You can split on spaces, map the "uppercasing" function to each piece, and join them back together.

var font_name = $(".font").val();
font_name = font_name.split(" ")
                     .map(function(a) {return a.charAt(0).toUpperCase()+a.substr(1);})
                     .join(" ");

Alternatively, see ucwords by PHPJS




回答3:


In plain javascript:

function firstcap(str) {
  var len = str.length;
  var re = /^\s$/;
  var special = true; // signalizes whether previous char is special or not
  for (var i=0; i<len; i++) {
    var code = str.charCodeAt(i);
    if (code>=97 && code<=122 && special) {
      str = str.substr(0, i) + String.fromCharCode(code-32) + str.substr(i+1);
      special = false;
    }
    else if (re.test(str[i])) {
      special = true;
    }
  }
  return str;
}

alert(firstcap('lucida sans'));



回答4:


var str = "hello world";
str = str.toLowerCase().replace(/\b./g, function(letter) {
    return letter.toUpperCase();
});
alert(str);

from here stack




回答5:


Here's a JavaScript function I just finished writing. Since I first checked StackOverflow to see if this JavaScript was around, but wasn't easily found, I'll post it for you guys to use. Hope it helps you guys, if it does, uptick me :)

var FormatMyTitle = function(UnformattedData) {
    var OutgoingData = UnformattedData;
    if (UnformattedData.indexOf("") != -1) {
        var OptionNameParts = UnformattedData.split(' ');
        for (var i=0; i<OptionNameParts.length; i++) {
            var OptionNamePieces = OptionNameParts[i].split('');
            if (OptionNamePieces.length >= 1) {
                var FirstLetter = ''+OptionNamePieces[0]+'';
                OptionNamePieces[0] = FirstLetter.toUpperCase();
            }
            OptionNameParts[i] = OptionNamePieces.join('');
        }
        OutgoingData = OptionNameParts.join(' ');
    }
    return OutgoingData;
};

And you would use it like so, for instance, using your original variable(font_name):

var FormattedTitle = FormatMyTitle(font_name);


来源:https://stackoverflow.com/questions/11370026/javascript-capitalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!