Hola el_javi y Caricatos!
Parece que encontre la solucion. Solo que es un tanto grande
![Riendo](http://static.forosdelweb.com/fdwtheme/images/smilies/risa.png)
Advierto que los creditos no son mios. Yo simplemente hice unas pequeñas modificaciones. El codigo original lo pueden ver
aqui.
Código HTML:
<html>
<head>
<style type="text/css">
textarea, input {
behavior: url(selection.htc);
}
</style>
<script type="text/javascript">
function selectText(start,end)
{
var text = document.getElementById("test");
text.setSelectionRange(start, end);
}
</script>
</head>
<body>
<form>
<input id="test" type="text" value="CON CONTENIDO" onFocus="selectText(0,0)">
</form>
</body>
</html>
Codigo del archivo
selection.htc Código HTML:
<public:component>
<public:property name="selectionStart" get="getSelectionStart" />
<public:property name="selectionEnd" get="getSelectionEnd" />
<public:attach event="onfocus" handler="storeSelection" />
<public:attach event="onselect" handler="storeSelection" />
<public:attach event="onclick" handler="storeSelection" />
<public:attach event="onkeyup" handler="storeSelection" />
<public:method name="setSelectionRange" />
<script type="text/javascript">
var selection = null;
var selectionStart = -1;
var selectionEnd = -1;
function storeSelection(noRecurse)
{
// Иногда document.selection устанавливается правильно лишь
// спустя некоторое время, приходится это учитывать
if (!noRecurse)
setTimeout(function() {storeSelection(1)}, 500);
if (element.document.selection.type != "None" && element.document.selection.type != "Text")
return;
var range = element.document.selection.createRange();
if (range.parentElement() != element)
return;
selection = range.duplicate();
}
function getSelectionStart()
{
updateVars()
return selectionStart;
}
function getSelectionEnd()
{
updateVars();
return selectionEnd;
}
function updateVars()
{
if (!selection)
return;
selectionStart = selectionEnd = -1;
// Поправка на странный глюк при определении позиции в конце однострочного текста
if (element.value.indexOf("\r") < 0)
{
var wholeRange = element.document.body.createTextRange();
wholeRange.moveToElementText(element);
if (!selection.compareEndPoints("startToEnd", wholeRange))
selectionStart++;
if (!selection.compareEndPoints("endToEnd", wholeRange))
selectionEnd++;
}
selectionStart -= selection.moveStart("character", -element.value.length);
selectionEnd -= selection.moveEnd("character", -element.value.length);
selection = null;
// Вводим поправку на то, что знак \r не учитывается
var pos = -1;
do
{
pos = element.value.indexOf("\r", pos + 1);
if (pos >= 0 && selectionStart > pos)
selectionStart++;
if (pos >= 0 && selectionEnd > pos)
selectionEnd++;
} while (pos >= 0 && pos < selectionEnd);
}
function setSelectionRange(startPos, endPos)
{
if (startPos > endPos)
startPos = endPos;
// Вводим поправку на то, что знак \r не учитывается
var startCorrection = element.value.substr(0, startPos).match(/\r/g);
startCorrection = startCorrection ? startCorrection.length : 0;
var endCorrection = element.value.substr(0, endPos).match(/\r/g);
endCorrection = endCorrection ? endCorrection.length : 0;
selection = element.createTextRange();
selection.collapse(true);
selection.moveEnd("character", endPos - endCorrection);
selection.moveStart("character", startPos - startCorrection);
selection.select();
// Это нужно для случая, когда startPos == endPos
storeSelection();
}
</script>
</public:component>
Algunas observaciones. En IE funciona bien
![de acuerdo](http://static.forosdelweb.com/fdwtheme/images/smilies/dedosarriba.png)
En realidad, la finalidad principal de este codigo, es la selección de un texto determinado en un
input o
textarea. Por lo tanto,
no es una solucion optima, a mi parecer. Como variante podria usarse el objeto
selection.