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í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