限制文本区域中单行的行数和字母数
Limiting number of lines & letters in single line in textarea
对于 IE8 浏览器,我试图将文本区域中的行数限制为 20,并将每行中的字符数限制为 15。我尝试了 Whosebug 上已经可用的解决方案,例如 ,但它们都适用于 chrome 和其他不支持 IE8 的现代浏览器。有兼容IE8的解决方案吗?
我使用了两个答案的组合 ( & ) 找到了我的问题的解决方案,下面是最终解决方案
function getInputSelection(el) {
var start = 0, normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
start = el.selectionStart;
} else {
range = document.selection.createRange();
if (range && range.parentElement() === el) {
normalizedValue = el.value.replace(/\r\n/g, "\n");
len = normalizedValue.length;
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
}
}
}
return start;
}
$(document).ready(function() {
//Restrict the search
var textArea = $('#textarea_id');
var maxRows = 30;
var maxChars = 17;
textArea.keypress(function(e) {
var text = textArea.val();
var lines = text.split('\n');
if (e.keyCode === 13) {
return lines.length < maxRows;
} else { //Should check for backspace/del/etc.
var caret = getInputSelection(textArea.get(0));
var line = 0;
var charCount = 0;
$.each(lines, function(i, e) {
charCount += e.length;
if (caret <= charCount) {
line = i;
return false;
}
//\n count for 1 char;
charCount += 1;
});
var theLine = lines[line];
return theLine.length < maxChars;
}
});
});
对于 IE8 浏览器,我试图将文本区域中的行数限制为 20,并将每行中的字符数限制为 15。我尝试了 Whosebug 上已经可用的解决方案,例如 ,但它们都适用于 chrome 和其他不支持 IE8 的现代浏览器。有兼容IE8的解决方案吗?
我使用了两个答案的组合 ( & ) 找到了我的问题的解决方案,下面是最终解决方案
function getInputSelection(el) {
var start = 0, normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
start = el.selectionStart;
} else {
range = document.selection.createRange();
if (range && range.parentElement() === el) {
normalizedValue = el.value.replace(/\r\n/g, "\n");
len = normalizedValue.length;
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
}
}
}
return start;
}
$(document).ready(function() {
//Restrict the search
var textArea = $('#textarea_id');
var maxRows = 30;
var maxChars = 17;
textArea.keypress(function(e) {
var text = textArea.val();
var lines = text.split('\n');
if (e.keyCode === 13) {
return lines.length < maxRows;
} else { //Should check for backspace/del/etc.
var caret = getInputSelection(textArea.get(0));
var line = 0;
var charCount = 0;
$.each(lines, function(i, e) {
charCount += e.length;
if (caret <= charCount) {
line = i;
return false;
}
//\n count for 1 char;
charCount += 1;
});
var theLine = lines[line];
return theLine.length < maxChars;
}
});
});