Foros del Web » Programando para Internet » Javascript »

Selección de texto en div

Estas en el tema de Selección de texto en div en el foro de Javascript en Foros del Web. Hola Bueno explico el problema que tengo estoy haciendo una aplicación web, que tiene un opcion que es marca el texto que hay dentro de ...
  #1 (permalink)  
Antiguo 18/03/2011, 04:30
 
Fecha de Ingreso: marzo-2011
Ubicación: Madrid
Mensajes: 4
Antigüedad: 13 años, 1 mes
Puntos: 0
Selección de texto en div

Hola

Bueno explico el problema que tengo estoy haciendo una aplicación web, que tiene un opcion que es marca el texto que hay dentro de un div, el usuario con el ratón ira seleccionado el texto que le interesa dentro del div y que daria marcado cambiando el color del fondo del texto que ha seleccionado. he buscado no he encontrado nada que me sirva para hacer lo que yo necesito, no pido que me den el codigo javascript sino ideas o q atributos o clase utilizar para poner hacer lo que necesito

Adios

Saludos.
  #2 (permalink)  
Antiguo 18/03/2011, 08:27
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: Selección de texto en div

Probá así:
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">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Documento sin t&#237;tulo</title>
<script type="text/javascript">
function 
colorear(){
    if(
typeof document.selection != 'undefined' && document.selection) {
        var 
str document.selection.createRange().text;
        var 
sel document.selection.createRange();
        
sel.text '<span style="background:yellow">' str '</span>';
        
this.innerHTML=this.innerText;
        
document.selection.empty() ;
        return;

    }else if(
window.getSelection){
        var 
sel=window.getSelection();
        var 
span=document.createElement('span');
        
span.style.background='yellow';
        
span.innerHTML=sel;
        var 
range sel.getRangeAt(0);
        
range.deleteContents();
        
range.insertNode(span);
    }
}
</script>

</head>

<body>


<div id="pp" onmouseup="colorear.call(this)">Seleccione total o parcialmente este texto</div>
</body>
</html> 
O así, para que funcione exactamente igual en cualquier navegador:
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=utf-8" />
<title>Documento sin título</title>
<script type="text/javascript">
function colorear(){
	if(typeof document.selection != 'undefined' && document.selection) {
		var str = document.selection.createRange().text;
		var sel = document.selection.createRange();
		sel.text = '{{' + str + '}}';
		this.innerHTML=this.innerHTML.replace(/\{\{[^\}\}]*\}\}/g, function(key){return '<span style="background:yellow">'+key.slice(2,-2)+'</span>'});
		document.selection.empty() ;
		return;
	}else if(window.getSelection){
		var sel=window.getSelection();
		var span=document.createElement('span');
		span.style.background='yellow';
		span.innerHTML=sel;
		var range = sel.getRangeAt(0);
		range.deleteContents();
		range.insertNode(span);
	}
}
</script>

</head>

<body>


<div id="pp" onmouseup="colorear.call(this)">Seleccione total o parcialmente este texto</div>
</body>
</html>
También te recomiendo echar un vistazo a esto: http://www.disegnocentell.com.ar/notas2.php?id=206

Última edición por Panino5001; 18/03/2011 a las 15:19
  #3 (permalink)  
Antiguo 18/03/2011, 08:36
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: Selección de texto en div

Otro enlace que quizá te interese:
http://css-tricks.com/overriding-the...olor-with-css/

Etiquetas: Ninguno
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 12:37.