javascript 将某个类中每个单词的首字母大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8730037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capitalise the first letter of each word within a certain class
提问by Iain Simpson
Is it possible to capitalise the first letter of each word in a certain class name using jQuery / javascript? I just want to capitalise the first letter of each word of all the fields marked with the class 'capital'.
是否可以使用 jQuery / javascript 将某个类名中每个单词的第一个字母大写?我只想将所有标有“大写”类的字段的每个单词的第一个字母大写。
I just want it to do it as they type, and I know you can do it with css but this is no good as it is stored in the DB as lowercase still.
我只是想让它在他们输入的时候做,我知道你可以用 css 来做,但这并不好,因为它仍然以小写形式存储在数据库中。
回答by jabclab
Here's a simple jQuery plugin that could do this for you:
这是一个简单的 jQuery 插件,可以为您执行此操作:
$.fn.capitalise = function() {
return this.each(function() {
var $this = $(this),
text = $this.text(),
tokens = text.split(" ").filter(function(t) {return t != ""; }),
res = [],
i,
len,
component;
for (i = 0, len = tokens.length; i < len; i++) {
component = tokens[i];
res.push(component.substring(0, 1).toUpperCase());
res.push(component.substring(1));
res.push(" "); // put space back in
}
$this.text(res.join(""));
});
};
And then call like:
然后像这样调用:
$(".myClass").capitalise();
Here's a working example.
这是一个工作示例。
回答by Dutchie432
The solution is something like this:
解决方案是这样的:
Working Sample: http://jsfiddle.net/Py7rW/7/
工作示例:http: //jsfiddle.net/Py7rW/7/
$('.captial').each(function(){
var arr = $(this).text().split(' ');
var result = "";
for (var x=0; x<arr.length; x++)
result+=arr[x].substring(0,1).toUpperCase()+arr[x].substring(1)+' ';
$(this).text(result.substring(0, result.length-1));
});
回答by Alex Dn
I think this will work :)
我认为这会奏效:)
$('.capital').css("text-transform","capitalize");
$('.capital').css("text-transform","capitalize");
回答by techfoobar
You can try something like:
您可以尝试以下操作:
$('.capital').each(function() {
var s = $(this).text().split(' ');
for(var i=0; i<s.length; i++) {
s[i] = s[i].substring(0,1).toUpperCase() + s[i].substring(1);
}
s = s.join(' ');
$(this).text(s);
}
回答by kennebec
I would use the css text-transform:capitalize to avoid having to run this on every keypress, and change the actual value of the fields on change.
我会使用 css text-transform:capitalize 以避免在每次按键时都运行它,并在更改时更改字段的实际值。
field.value= field.value.replace(/((^| )[a-z])/g, function(a, b){
return b.toUpperCase();
});
回答by Irshad Khan
Simple Step to capitalize the first letter of each word :
将每个单词的第一个字母大写的简单步骤:
$(document).on('keyup', '#myText', function () {
this.value = this.value.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
});
回答by vcsjones
You could do something like this. This will capitalize the text in a textbox whenever the text has changed:
你可以做这样的事情。每当文本更改时,这将大写文本框中的文本:
$(document).ready(function() {
$('.capital').change(function() {
var arr = $(this).val().split(' ');
var result = "";
for (var i=0; i<arr.length; i++){
result += arr[i].substring(0,1).toUpperCase() + arr[i].substring(1);
if (i < arr.length-1) {
result += ' ';
}
}
$(this).val(result);
})
});
You can see a working fiddle here: http://jsfiddle.net/5dMg7/
你可以在这里看到一个工作小提琴:http: //jsfiddle.net/5dMg7/