Ver Mensaje Individual
  #17 (permalink)  
Antiguo 01/07/2008, 18:16
Avatar de derkenuke
derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 21 años, 4 meses
Puntos: 45
Respuesta: Palabras Claves y Textarea

Hola de nuevo:

Mira este ejemplo, es sencillo el funcionamiento una vez tienes las funciones definidas. A ponCursorEnPos() le falta pasarle por argumento laCaja, que lo tenemos definido como variable global, pero no cuesta nada.

Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<
head>
<
meta http-equiv="Content-type" content="text/html;charset=iso-8859-1" />
<
meta name="Author" content="derkeNuke" />
<
title>Página nueva</title>
<
style type="text/css">

</
style>
</
head>

<
body>

<
textarea id="txtArea" onclick="escribe( getCursorPos(this).start ); ponCursorEnPos(2)">hola que tal</textarea>

<
script type="text/javascript">
<!--


// escribir en el documento una ristra (x)html fuera de tiempo de ejecución.
function escribe(q,sinBR) {
    var 
elDIV document.createElement("DIV");
    if(
q==undefinedq="";
    if(!
sinBRq+="<br/>";
    
elDIV.innerHTML q;
    for(var 
a=0elDIV.childNodes.length>aa++) {
        
document.body.appendChildelDIV.childNodes[a].cloneNode(true) );
    }
}
// document.getElementById abreviado
function $(x) { return document.getElementById(x); }

var 
laCaja = $("txtArea");

/********
    getCursorPos: Me devuelve la posición de inicio y fin de la selección o la posición del | en el campo
********/
function getCursorPos(campo) {
    if (
document.selection) {// IE Support
        
campo.focus();                                        // Set focus on the element
        
var oSel document.selection.createRange();        // To get cursor position, get empty selection range
        
oSel.moveStart('character', -campo.value.length);    // Move selection start to 0 position
        
campo.selectionEnd oSel.text.length;                    // The caret position is selection length
        
oSel.setEndPoint('EndToStart'document.selection.createRange() );
        
campo.selectionStart oSel.text.length;
    }
    return { 
startcampo.selectionStartendcampo.selectionEnd };
}  

/********
    COLOCACIÓN DEL CURSOR: Da el foco a la caja colocando el cursor de inserción en la posición pos
********/
function ponCursorEnPos(pos){ 
    if(
typeof document.selection != 'undefined' && document.selection){        //método IE
        
var tex=laCaja.value;
        
laCaja.value=''
        
laCaja.focus();
        var 
str document.selection.createRange(); 
        
laCaja.value=tex;
        
str.move("character"pos); 
        
str.moveEnd("character"0); 
        
str.select();
    }
    else if(
typeof laCaja.selectionStart != 'undefined'){                    //método estándar
        
laCaja.setSelectionRange(pos,pos); 
        
forzar_focus();            //debería ser focus(), pero nos salta el evento y no queremos
    
}


// -->
</script>

</body>
</html> 

Un saludo, y con esto espero que te salga.
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.