我想在使用javascript在textarea中输入时,用Devanagari Danda(。)替换Vertical Bar(|)。
首先,我尝试了在How to change characters typed in Firefox上给出的解决方案。但它只是将角色添加到最后。
所以,我遵循了http://www.jsfiddle.net/EXH2k/6/给出的解决方案,这是Tim Down在Changing the keypress以及show different keyboard character from the typed one in google chrome上提出的建议。但它对我不起作用(无论是在Firefox还是在IE 10中)。
代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="stylesheet.css" />
<script type="text/javascript">
function transformTypedChar(charStr) {
return charStr == "|" ? "।" : charStr;
}
function getInputSelection(el) {
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
// 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 = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
function offsetToRangeCharacterMove(el, offset) {
return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
}
function setInputSelection(el, startOffset, endOffset) {
el.focus();
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
el.selectionStart = startOffset;
el.selectionEnd = endOffset;
} else {
var range = el.createTextRange();
var startCharMove = offsetToRangeCharacterMove(el, startOffset);
range.collapse(true);
if (startOffset == endOffset) {
range.move("character", startCharMove);
} else {
range.moveEnd("character", offsetToRangeCharacterMove(el, endOffset));
range.moveStart("character", startCharMove);
}
range.select();
}
}
$("#inputTextArea").keypress(function (evt) {
if (evt.which) {
var charStr = String.fromCharCode(evt.which);
var transformedChar = transformTypedChar(charStr);
if (transformedChar != charStr) {
var sel = getInputSelection(this), val = this.value;
this.value = val.slice(0, sel.start) + transformedChar + val.slice(sel.end);
// Move the caret
setInputSelection(this, sel.start + 1, sel.start + 1);
return false;
}
}
});
</script>
</head>
<body>
<textarea name="input" id="inputTextArea" rows="10"></textarea>
</body>
</html>
答案 0 :(得分:5)
这很简单。使用新行处理等丢弃您的代码,然后执行此操作:
$(document).ready(function() {
$('#my-input').on('input', function() {
var $input = $(this),
curVal = $input.val();
var cursorPos = curVal.slice(0, this.selectionStart).length;
$input.val(curVal.replace(/[|]/g, '।'));
this.setSelectionRange(cursorPos, cursorPos);
});
});
工作示例:JSFiddle
答案 1 :(得分:1)
我想尽快用Devanagari Danda(。)替换Vertical Bar(|) 因为它是使用javascript在textarea中键入的。
由于您希望在键入字符后立即替换字符,这可能是最简单的选项:
$('#inputTextArea').on("keyup", function(e) {
$(this).val($(this).val().replace(/[|]/g, "।"));
});
我注意到虽然你没有标记jQuery,但你在代码中使用了jQuery。因此,基于jQuery的解决方案。