请我在开始输入input
时需要根据css
的条件(例如text-align:right
或'direction:rtl
)自动将用户的语言更改为阿拉伯语而不是英语
我发现下面的脚本可以做到这一点,但是由于我是初学者,因此我无法对其进行修改以使其与下面的input text
或任何其他输入一起使用。
function convert2Ar(id, lang) {
if (lang == 'ar') {
var convert = true;
var selectall = false;
var cursorindx = 0;
$('#' + id).bind('keydown', function (e) {
if (e.shiftKey && e.altKey) {
convert = false;
}
if (e.keyCode == 65 && e.ctrlKey) {
selectall = true;
}
});
$('#' + id).bind('keypress', function (e) {
if (selectall) {
this.select();
selectall = false;
}
else if (e.keyCode != 8 && e.which != 0 && convert) {
console.log(e.keyCode);
e.preventDefault();
cursorindx = this.selectionStart;
var txt = $(this).val();
txt = txt.substring(0, this.selectionStart) + char2Ar(String.fromCharCode(e.which), e) + txt.substring(this.selectionEnd, txt.length);
$(this).val(txt);
this.selectionStart = cursorindx + 1;
this.selectionEnd = cursorindx + 1;
}
})
}
}
function char2Ar(c, e) {
if (e.keyCode != 16 && e.keyCode != 18) {
var letter = c;
if (e.shiftKey) {
switch (c) {
case "A": letter = "ِ"; break;
case "B": letter = "لآ"; break;
case "C": letter = "}"; break;
case "D": letter = "]"; break;
case "E": letter = "ُ"; break;
case "F": letter = "["; break;
case "G": letter = "لأ"; break;
case "H": letter = "أ"; break;
case "I": letter = "÷"; break;
case "J": letter = "ـ"; break;
case "K": letter = "،"; break;
case "L": letter = "/"; break;
case "M": letter = "’"; break;
case "N": letter = "آ"; break;
case "O": letter = "×"; break;
case "P": letter = "؛"; break;
case "Q": letter = "َ"; break;
case "R": letter = "ٌ"; break;
case "S": letter = "ٍ"; break;
case "T": letter = "لإ"; break;
case "U": letter = "‘"; break;
case "V": letter = "{"; break;
case "W": letter = "ً"; break;
case "X": letter = "ْ"; break;
case "Y": letter = "إ"; break;
case "Z": letter = "~"; break;
case "?": letter = "؟"; break;
case "~": letter = "ّ"; break;
}
}
else {
switch (c) {
case ";": letter = "ك"; break;
case "a": letter = "ش"; break;
case "b": letter = "لا"; break;
case "c": letter = "ؤ"; break;
case "d": letter = "ي"; break;
case "e": letter = "ث"; break;
case "f": letter = "ب"; break;
case "g": letter = "ل"; break;
case "h": letter = "ا"; break;
case "i": letter = "ه"; break;
case "j": letter = "ت"; break;
case "k": letter = "ن"; break;
case "l": letter = "م"; break;
case "m": letter = "ة"; break;
case "n": letter = "ى"; break;
case "o": letter = "خ"; break;
case "p": letter = "ح"; break;
case "q": letter = "ض"; break;
case "r": letter = "ق"; break;
case "s": letter = "س"; break;
case "t": letter = "ف"; break;
case "u": letter = "ع"; break;
case "v": letter = "ر"; break;
case "w": letter = "ص"; break;
case "x": letter = "ء"; break;
case "y": letter = "غ"; break;
case "z": letter = "ئ"; break;
case ",": letter = "و"; break;
case ".": letter = "ز"; break;
case "/": letter = "ظ"; break;
case "`": letter = "ذ"; break;
case "'": letter = "ط"; break;
case "[": letter = "ج"; break;
case "]": letter = "د"; break;
};
}
return letter;
}
}
<input type="text" id="txt_MenuSearch" class="search-input ui-autocomplete-input" placeholder="search here.." autocomplete="off">
答案 0 :(得分:1)
我的意思是您的代码是一个函数,需要这样调用:
convert2Ar('txt_MenuSearch', 'ar');
但是整个想法对我来说很奇怪,您想要实现什么?为什么不让用户更改其键盘布局?
答案 1 :(得分:0)