我決定打開一個新問題,因爲我試圖大寫第一個帶有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