Ver Mensaje Individual
  #4 (permalink)  
Antiguo 14/06/2012, 12:31
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 17 años, 7 meses
Puntos: 1567
Respuesta: Seleccionar texto y comparar

getSelection() no se comporta igual en todos los navegadores, tendrías que hacerlo así

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <title>selección de texto</title>
  5. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  6. <script type="text/javascript">
  7. textoSeleccionado = function(){
  8. var t = '';
  9. if(window.getSelection){
  10. t = window.getSelection();
  11. }else if(document.getSelection){
  12. t = document.getSelection();
  13. }else if(document.selection){
  14. t = document.selection.createRange().text;
  15. }
  16. return t;
  17. }
  18.            
  19. mouseup = function(){
  20. var tsel = textoSeleccionado();
  21. if(tsel!=''){
  22. alert("Texto Seleccionado:\n"+tsel);
  23.  
  24. /// Buscar coincidencia
  25. var items = new Array("ingredere", "constanter", "civitas");
  26. for (var i = 0; i < items.length; i++) {
  27.  var item = items[i];
  28. // alert(item);
  29. var coincidencia = new RegExp(item,"i");
  30. var existe= coincidencia.test(tsel);
  31. if(existe == true){
  32. alert(items[i] + ' está en el texto seleccionado');
  33. }
  34.  
  35. }
  36.  
  37.  
  38. }
  39. }
  40.        
  41. $(document).ready(function(){
  42. $(document).bind("mouseup",mouseup);
  43. });
  44.                        
  45. </head>
  46. <p>
  47. Lorem ipsum dolor sit amet, re ingredere tempore percussus ait. Scola veniamus est amet amet constanter approximavit te.
  48. Maria non ait regem Boreas ingreditur ipse a civitas ex sic ut sua etiam quantitas non coepit.
  49. </p>
  50. </body>
  51. </html>

Lo que está medio confuso es tu enunciado
Cita:
Quiero que al seleccionar alguna parte de texto, aparezca un alert que me indique si esa palabra ó palabras existen o no en el array.
yo lo interpreto, si alguna o algunas palabras de la lista está en el texto seleccionado, te mande un alert

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.