通过javascript将焦点设置为输入标记的末尾
敬礼。我对javascript有一些问题 我制作了翻译脚本,允许格鲁吉亚用户按拉丁字母输入格鲁吉亚语。 因此,当我在输入标记上操作时,我无法将焦点设置为文本的结尾。。正确地说,光标位于文本的末尾,但如果文本比输入标记大得多,它必须自动滚动并且不能隐藏。 这是密码通过javascript将焦点设置为输入标记的末尾,javascript,jquery,html,string,input,Javascript,Jquery,Html,String,Input,敬礼。我对javascript有一些问题 我制作了翻译脚本,允许格鲁吉亚用户按拉丁字母输入格鲁吉亚语。 因此,当我在输入标记上操作时,我无法将焦点设置为文本的结尾。。正确地说,光标位于文本的末尾,但如果文本比输入标记大得多,它必须自动滚动并且不能隐藏。 这是密码 $(document).ready(function() { $(".geok").geokbd(); }); $.fn.geokbd = function() { symbols = "abgdevzTiklmnopJ
$(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;
}
}
}
);
}
$(文档).ready(函数(){
$(“.geok”).geokbd();
});
$.fn.geokbd=函数(){
symbols=“abgdevzTiklmnopJrstufqRySCcZwWxjh”;
this.relatedCheckbox=$(“”).attr('checked',true);
$(this.after)(this.relatedCheckbox);
$(这个)。按键(
职能(e){
如果((e.charCode)==96){
if($(this).next('input').attr('checked')){
$(this).next('input').attr('checked',false);
}否则{
$(this).next('input').attr('checked',true);
}
返回false;
}
if($(this).next('input').attr('checked')){
if((index=symbols.indexOf(String.fromCharCode(e.charCode)))>=0){
ret=String.fromCharCode(索引+4304);
这是focus();
var scrollTop=this.scrollTop,
开始=此。选择开始,
end=this.selectionEnd;
var value=this.value.substring(0,start)+ret+this.value.substring(end,this.value.length);
这个值=值;
this.scrollTop=scrollTop;
this.selectionStart=this.selectionEnd=start+ret.length;
返回false;
}
}
}
);
}
谢谢获取最后一个输入标记的
id
,并用该id重新保存名称
,下面是基本逻辑
<script type="text/javascript">
function setFocus()
{
document.getElementById("name").focus();
}
</script>
</head>
<body onload="setFocus()">
<form>
Name: <input type="text" id="name" size="30"><br />
Surname: <input type="text" id="surname" size="30">
</form>
</body>
函数setFocus()
{
document.getElementById(“name”).focus();
}
名称:
姓:
您是否尝试过以下方法:
function doSetCaretPosition (oField, iCaretPos) {
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus ();
// Create empty selection range
var oSel = document.selection.createRange();
// Move selection start and end to 0 position
oSel.moveStart ('character', -oField.value.length);
// Move selection start and end to desired position
oSel.moveStart ('character', iCaretPos);
oSel.moveEnd ('character', 0);
oSel.select ();
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0') {
oField.selectionStart = iCaretPos;
oField.selectionEnd = iCaretPos;
oField.focus ();
}
}
$(document).ready(function() {
$("#yourElement").focus();
doSetCaretPosition(document.getElementById("yourElement"),999);
});
(来源:)有几个很好的答案 例如,您可以使用
<input id="search" type="text" value="mycurrtext" size="30"
onfocus="this.value = this.value;" name="search"/>
或者您可以使用JQuery插件:PutCursorAtEnd。作者甚至在那里发布了源代码,我测试了它是否适用于Opera。阅读整个问题,而不仅仅是标题。据我所知,原因是我手动为输入元素插入值,并在按键时返回false。如果我返回true,则一切正常,但不需要符号。我不想将焦点设置在文档上。准备。。我要用按键。所以,它不起作用