gpt4 book ai didi

javascript - 在 textarea 中输入时,将垂直条 ("|") 替换为 Devanagari Danda ("।")

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:57 25 4
gpt4 key购买 nike

我想在使用 javascript 在 textarea 中输入竖线 (|) 后立即将其替换为 Devanagari Danda (।)。

首先我尝试了 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>

最佳答案

这很简单。用换行符处理等丢弃你的代码,然后这样做:

$(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

关于javascript - 在 textarea 中输入时,将垂直条 ("|") 替换为 Devanagari Danda ("।"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19153640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com