问候,我想把脚本中的每个单词都大写,为此我想出了一个这样的方法:
//Word Capitalization
function wordToUpper(val) {
newVal = '';
val = val.toLowerCase().split(' ');
for(var c=0; c < val.length; c++) {
newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
}
return newVal;
}
Run Code Online (Sandbox Code Playgroud)
现在它适用于从之后开始的常规单词和emtpy char"".
但是我也想确保它对这样的字符串失败:
wordToUpper('hello my name is Hellnar.it doesnt work.')- > Hello My Name Is Hellnar.it Doesnt Work."它"必须是资本.
wordToUpper('hello my name is (hellnar).') - > Hello My Name Is (hellnar). "Hellnar"不得不资本.
问候
注意:请不要使用css经典text-transform: capitalize; 解决方案,因为此数据将用于表单发布.