Programowy wybór częściowego tekstu w polu wprowadzania
Jak programowo wybrać określony zakres tekstu w polu HTML input
? (Nie chcę wybierać całego pola, tylko podzbiór)
Ponadto, jak Mogę określić aktualnie wybrany zakres w polu?
41
Author: Tony the Pony, 2009-03-14
3 answers
Oto jak wybrać część pola tekstowego (range select) i pobrać zaznaczony tekst:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
window.onload = function() {
var message = document.getElementById('message');
// Select a portion of text
createSelection(message, 0, 5);
// get the selected portion of text
var selectedText = message.value.substring(message.selectionStart, message.selectionEnd);
alert(selectedText);
};
function createSelection(field, start, end) {
if( field.createTextRange ) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end);
selRange.select();
field.focus();
} else if( field.setSelectionRange ) {
field.focus();
field.setSelectionRange(start, end);
} else if( typeof field.selectionStart != 'undefined' ) {
field.selectionStart = start;
field.selectionEnd = end;
field.focus();
}
}
</script>
</head>
<body>
<input type="text" name="message" id="message" value="Hello World" />
</body>
</html>
53
Author: Darin Dimitrov,
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
2013-03-14 15:13:29
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
2013-03-14 15:13:29
Mała korekta. Wygląda na to, że metoda IE moveEnd() porusza się stopniowo, więc selRange.moveEnd('character', end)
należy zastąpić selRange.moveEnd('character', end-start)
:
function createSelection(field, start, end) {
if( field.createTextRange ) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end-start);
selRange.select();
} else if( field.setSelectionRange ) {
field.setSelectionRange(start, end);
} else if( field.selectionStart ) {
field.selectionStart = start;
field.selectionEnd = end;
}
field.focus();
}
13
Author: Luis Perez,
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-11-09 00:41:13
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-11-09 00:41:13
Dziękuję! Chcę teraz udostępnić swoją funkcję, jest używana w AJAXEL CMS wraz z komunikatorem
,wrapText:function(o, ot, ct) {
var s = o[0].selectionStart;
var e = o[0].selectionEnd;
o.val(o.val().substring(0, s)+ot+o.val().substring(s,e)+ct+o.val().substring(e, o.val().length));
if (o[0].createTextRange){
var sr = o[0].createTextRange();
sr.collapse(true);
sr.moveStart('character', s);
sr.moveEnd('character',e-s+ot.length+ct.length);
sr.select();
}
else if(o[0].setSelectionRange){
o[0].setSelectionRange(s,e+ot.length+ct.length);
}
else if(o[0].selectionStart){
o[0].selectionStart=s;
o[0].selectionEnd=e+ot.length+ct.length;
}
}
4
Author: Alexander,
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
2011-12-29 14:08:28
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
2011-12-29 14:08:28