通过javascript将焦点设置到输入标记的末尾
敬礼。我的 javascript 有一些问题。
我制作了翻译脚本,允许格鲁吉亚用户通过拉丁字母输入格鲁吉亚语。 因此,当我操作输入标记时,我无法将焦点设置到文本末尾。正确地,光标位于文本末尾,但如果文本比输入标记大得多,则它必须自动滚动并且不能隐藏。
这是代码..
$(document).ready(function() {
$(".geok").geokbd();
});
$.fn.geokbd = function() {
symbols = "abgdevzTiklmnopJrstufqRySCcZwWxjh";
this.relatedCheckbox = $("<input type=\"checkbox\" name=\"geokbd\" />").attr('checked', true);
$(this).after(this.relatedCheckbox);
$(this).keypress(
function(e) {
if ((e.charCode) == 96) {
if ($(this).next('input').attr('checked')) {
$(this).next('input').attr('checked', false);
} else {
$(this).next('input').attr('checked', true);
}
return false;
}
if ($(this).next('input').attr('checked')) {
if ((index = symbols.indexOf(String.fromCharCode(e.charCode))) >= 0) {
ret = String.fromCharCode(index + 4304);
this.focus();
var scrollTop = this.scrollTop,
start = this.selectionStart,
end = this.selectionEnd;
var value = this.value.substring(0, start) + ret + this.value.substring(end,this.value.length);
this.value = value;
this.scrollTop = scrollTop;
this.selectionStart = this.selectionEnd = start + ret.length;
return false;
}
}
}
);
}
谢谢
Salut. i have some problem with javascript..
I made translate script, that allows Georgian users type Georgian by Latin alphabet.
So, when i manipulate on input tag, i can't set focus to the end of the text.. correctly, cursor is in the end of text but if text is much bigger than input tag, it must scroll automatically and must not hide.
here's code..
$(document).ready(function() {
$(".geok").geokbd();
});
$.fn.geokbd = function() {
symbols = "abgdevzTiklmnopJrstufqRySCcZwWxjh";
this.relatedCheckbox = $("<input type=\"checkbox\" name=\"geokbd\" />").attr('checked', true);
$(this).after(this.relatedCheckbox);
$(this).keypress(
function(e) {
if ((e.charCode) == 96) {
if ($(this).next('input').attr('checked')) {
$(this).next('input').attr('checked', false);
} else {
$(this).next('input').attr('checked', true);
}
return false;
}
if ($(this).next('input').attr('checked')) {
if ((index = symbols.indexOf(String.fromCharCode(e.charCode))) >= 0) {
ret = String.fromCharCode(index + 4304);
this.focus();
var scrollTop = this.scrollTop,
start = this.selectionStart,
end = this.selectionEnd;
var value = this.value.substring(0, start) + ret + this.value.substring(end,this.value.length);
this.value = value;
this.scrollTop = scrollTop;
this.selectionStart = this.selectionEnd = start + ret.length;
return false;
}
}
}
);
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获取最后一个输入标签的
id
并用该 id 替换name
,以下是基本逻辑get the
id
of last input tag and replavename
with that id , below is basic logic您尝试过吗:(
来源:http://www.webdeveloper .com/forum/archive/index.php/t-74982.html)
Have you tried this :
(source : http://www.webdeveloper.com/forum/archive/index.php/t-74982.html )
有几个很好的答案
使用 JavaScript 将光标放置在文本输入元素中的文本末尾
例如,您可以使用
或者您可以使用 JQuery 插件:PutCursorAtEnd。作者甚至在那里发布了源代码,我测试了这适用于 Opera。
There are several good answers from
Use JavaScript to place cursor at end of text in text input element
For example, you can use
Or you can use the JQuery plugin: PutCursorAtEnd. The author even posted the source code there and I tested that this works for Opera.