Foros del Web » Programando para Internet » Javascript »

obtener valor del texto seleccionado

Estas en el tema de obtener valor del texto seleccionado en el foro de Javascript en Foros del Web. Como puedo obtener el valor de un texto seleccionado?? Esto es: seleccionar un texto, que no esta en un formulario sino en el body y ...
  #1 (permalink)  
Antiguo 04/10/2009, 06:04
 
Fecha de Ingreso: octubre-2008
Mensajes: 41
Antigüedad: 15 años, 6 meses
Puntos: 0
obtener valor del texto seleccionado

Como puedo obtener el valor de un texto seleccionado?? Esto es: seleccionar un texto, que no esta en un formulario sino en el body y al hacer clic derecho aparezca un alert con ese texto
  #2 (permalink)  
Antiguo 04/10/2009, 12:40
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: obtener valor del texto seleccionado

Fijate si te sirve:
Código:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<script>
function seleccion(){
	if(window.getSelection!= 'undefined' && window.getSelection){
		return window.getSelection();
	}else if(document.selection != 'undefined' && document.selection ){
		return document.selection.createRange().text;
	}else{
		return null;
	}
}
function mostrar(e){
	var evt=e || event;
	if(evt.which == 3 || evt.button==2){
		alert(seleccion());
	}
}
function addEvent(obj,fun,type){
    if(obj.addEventListener){
        obj.addEventListener(type,fun,false);
    }else if(obj.attachEvent){
        var f=function(){
            fun.call(obj,window.event);
        }
        obj.attachEvent('on'+type,f);
        obj[fun.toString()+type]=f;
    }else{
        obj['on'+type]=fun;
    }
}
onload=function(){
	addEvent(document,mostrar,'mousedown');
	addEvent(document,function(e){if(e.preventDefault)e.preventDefault();else e.returnValue=false;},'contextmenu');
}
</script>
</head>

<body>
esto es un ejemplo 
</body>
</html>
  #3 (permalink)  
Antiguo 05/10/2009, 02:44
 
Fecha de Ingreso: octubre-2008
Mensajes: 41
Antigüedad: 15 años, 6 meses
Puntos: 0
Respuesta: obtener valor del texto seleccionado

Es justo eso!! muchísimas gracias!!
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 14:30.