Pole Wprowadzania Ostrości Przy Obciążeniu
Jak można skupić kursor na określonym polu wprowadzania podczas ładowania strony?
Czy możliwe jest zachowanie początkowej wartości tekstu i umieszczenie kursora na końcu wejścia?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
9 answers
Są dwie części twojego pytania.
1) Jak skupić dane wejściowe na ładowaniu strony?
Możesz po prostu dodać atrybut autofocus
do wejścia.
<input id="myinputbox" type="text" autofocus>
Jednak może to nie być obsługiwane we wszystkich przeglądarkach, więc możemy używać javascript.
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
2) Jak umieścić kursor na końcu wprowadzanego tekstu?
Oto rozwiązanie nie-jQuery z jakimś zapożyczonym kodem z innego so answer.
function placeCursorAtEnd() {
if (this.setSelectionRange) {
// Double the length because Opera is inconsistent about
// whether a carriage return is one character or two.
var len = this.value.length * 2;
this.setSelectionRange(len, len);
} else {
// This might work for browsers without setSelectionRange support.
this.value = this.value;
}
if (this.nodeName === "TEXTAREA") {
// This will scroll a textarea to the bottom if needed
this.scrollTop = 999999;
}
};
window.onload = function() {
var input = document.getElementById("myinputbox");
if (obj.addEventListener) {
obj.addEventListener("focus", placeCursorAtEnd, false);
} else if (obj.attachEvent) {
obj.attachEvent('onfocus', placeCursorAtEnd);
}
input.focus();
}
Oto przykład jak by to osiągnąć z jQuery.
<input type="text" autofocus>
<script>
$(function() {
$("[autofocus]").on("focus", function() {
if (this.setSelectionRange) {
var len = this.value.length * 2;
this.setSelectionRange(len, len);
} else {
this.value = this.value;
}
this.scrollTop = 999999;
}).focus();
});
</script>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:09
Tylko uwaga - teraz możesz to zrobić z HTML5 bez JavaScript dla przeglądarek, które go obsługują:
<input type="text" autofocus>
Prawdopodobnie chcesz zacząć od tego i budować na nim za pomocą JavaScript, aby zapewnić alternatywę dla starszych przeglądarek.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-22 22:48:46
$(document).ready(function() {
$('#id').focus();
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-02 22:00:17
Fragmenty kodu:
function focusOnMyInputBox(){
document.getElementById("myinputbox").focus();
}
<body onLoad="focusOnMyInputBox()")>
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-02 02:15:13
Przenośnym sposobem na to jest użycie niestandardowej funkcji (do obsługi różnic w przeglądarce), takiej jak Ta .
Następnie skonfiguruj obsługę onload
na końcu tagu <body>
, Jak napisał jessegavin:
window.onload = function() {
document.getElementById("myinputbox").focus();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:33
Jeśli z jakiegoś powodu nie możesz dodać do tagu BODY, możesz dodać to po formularzu:
<SCRIPT type="text/javascript">
document.yourFormName.yourFieldName.focus();
</SCRIPT>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-22 14:29:30
Działa dobrze...
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-14 09:59:51
To jest to, co działa dobrze dla mnie:
<form name="f" action="/search">
<input name="q" onfocus="fff=1" />
</form>
Fff będzie zmienną globalną, której nazwa jest absolutnie nieistotna i której celem będzie zatrzymanie ogólnego zdarzenia onload, aby wymusić skupienie na danym wejściu.
<body onload="if(!this.fff)document.f.q.focus();">
<!-- ... the rest of the page ... -->
</body>
From: http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-18 22:52:09
Spróbuj:
Javascript Pure:
[elem][n].style.visibility='visible';
[elem][n].focus();
Jquery:
[elem].filter(':visible').focus();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-21 10:55:57