javascript大写 [英] javascript capitalization
问题描述
var font_name = $(".font").val();
我的JavaScript代码中有此代码.在我的html中,我有一个类为.font
的输入表单.
I have this in my JavaScript code. In my html I have an input form with the class .font
.
我想将.font
中每个单词的首字母大写,例如,如果有人键入Lucida sans
,它将变成Lucida Sans
.我找不到适合您的jQuery方法,所以我想我必须使用实际的JavaScript,但我真的不知道如何.
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.
推荐答案
您不能只使用CSS吗?
Can you not just use CSS?
.font {
text-transform: capitalize;
}
在jQuery中:
$(".font").css("text-transform","capitalize");
这篇关于javascript大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!