我决定打开一个新问题,因为我试图大写第一个带有4个或更多字母的单词并除少数关键词和我对这个工作代码:http://jsfiddle.net/Q2tFx/11/在输入中设置插入位置以便在可以编辑内容时在关键字上设置大写字母
$.fn.capitalize = function() {
var wordsToIgnore = ["to", "and", "the", "it", "or", "that", "this"],
minLength = 3;
function getWords(str) {
return str.match(/\S+\s*/g);
}
this.each(function() {
var words = getWords(this.value);
$.each(words, function (i, word) {
// only continue if word is not in ignore list
if (wordsToIgnore.indexOf($.trim(word).toLowerCase()) == -1 && $.trim(word).length > minLength) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
});
this.value = words.join("");
});
};
$('.title').on('keyup', function() {
$(this).capitalize();
}).capitalize();
但是我有一个问题,而在“KEYUP”运行功能。 我无法在输入中间编辑某些内容,而无法将光标置于输入末尾。而我也不能“全选”。
我知道有解决方案来获得插入位置并照顾这样的事情,但我真的不知道如何将它们集成到我的代码中。
任何想法我怎么能做到这一点?
你想大写所有的单词或只是第一个o NE? –
@BlakePlumb我需要大写所有的单词。带有“blur”而不是“keyup”的代码正在按照我的需要工作。问题是我想在输入时大写“live”这个词,但是在输入中间我不能编辑“keyup”。 – Santiago