Foros del Web » Programando para Internet » Javascript »

Pregunta Sobre Detección de texto seleccionado

Estas en el tema de Pregunta Sobre Detección de texto seleccionado en el foro de Javascript en Foros del Web. Que tal, no se si sea muy torpe, pero no logro que funcione :S Alguien sabe si esta algo mal? Gracias de Antemano!! Cita: Iniciado ...
  #1 (permalink)  
Antiguo 03/03/2009, 12:02
 
Fecha de Ingreso: septiembre-2005
Mensajes: 1.289
Antigüedad: 18 años, 7 meses
Puntos: 3
Pregunta Sobre Detección de texto seleccionado

Que tal, no se si sea muy torpe, pero no logro que funcione :S Alguien sabe si esta algo mal? Gracias de Antemano!!

Cita:
Iniciado por Kaopectate Ver Mensaje
P: ¿Como puedo capturar el texto que está seleccionado dentro de mi página?

Créditos: Tomado de una respuesta de DanielRey

R: [ver ejemplo]

Código PHP:
<html>
 <
head>
  <
a name="#1">
  <
script language="JavaScript">
   function 
sele() {
    
a=document.selection.createRange();
    
alert("Tipo de selección: " document.selection.type "\nTexto seleccionado: " a.text);
   }
  
</script>
 </head>
 <body>
  <form name="frm">
   <table border="0">
    <tr>
     <td align="right">
      input:
     </td>
     <td>
      <input type="textInput" name="txt">
     </td>
    </tr>
    <tr>
     <td align="right">
      textarea:
     </td>
     <td>
      <textarea name="txtArea"></textarea>
     </td>
    </tr>
    <tr>
     <td align="right">
      Link:
     </td>
     <td>
      <a href="#1">Enlace</a>
     </td>
    </tr>
    <tr>
     <td align="right">
      Select:
     </td>
     <td>
      <select>
       <option>Uno</option>
       <option>Dos</option>
       <option>Tres</option>
      </select>
     </td>
    </tr>
    <tr>
     <td align="right">
      Párrafo:
     </td>
     <td>
      Esto es un párrafo de donde también se puede seleccionar texto.
     </td>
    </tr>
    <tr>
     <td align="center" colspan="2">
      <a href=JavaScript:sele()>Ver texto seleccionado</a> 
     </td>
    </tr>
  </form>
 </body>
</html> 
__________________
Wow! No se que decir...
  #2 (permalink)  
Antiguo 05/03/2009, 22:26
Avatar de sweetlydark  
Fecha de Ingreso: septiembre-2008
Mensajes: 391
Antigüedad: 15 años, 7 meses
Puntos: 10
Respuesta: Pregunta Sobre Detección de texto seleccionado

Ese es el código para IE para firefox vas a necesitar combinar:

Código:
if(document.frm.txtArea.selectionStart==undefined) return; //si es FF
var begin = document.frm.txtArea.value.substr(0, document.frm.txtArea.selectionStart); 
var selection = document.frm.txtArea.value.substr(document.frm.txtArea.selectionStart, document.frm.txtArea.selectionEnd - document.frm.txtArea.selectionStart); 
var end = document.frm.txtArea.value.substr(document.frm.txtArea.selectionEnd); 
document.frm.txtArea.value = begin + selection + end;
Para textarea, input y

Código:
var selObj = window.getSelection(); 
alert(selObj);
var selRange = selObj.getRangeAt(0);
alert(selRange);
para divs y otros
__________________
Por fin vuelvo a tener internet!
http://www.anidandoetiquetas.com/
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:52.