Seleção de texto com javascript
Olá pessoal,
boa tarde,
bom estou com uma dúvida a respeito do javascript. Estou selecionando um texto com o mouse, trabalhando com o javascript, pego esse texto selecionado e atribuo uma classe CSS "yellow", seguem o código abaixo:
HTML:
<p id="testeCopytext">
[indent=1]Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. [/indent]
</p>
<div id="append"></div>
CSS:
.yellow{
background-color: #FFFF00;
}
.white{
background-color: #FFFFFF;} JAVASCRIPT:
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
alert(window.getSelection.anchorNode)
}
else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
$("#testeCopytext").mouseup(function (e){
var SelectedText = getSelectionText();
var alltext = $(this).html();
var strSearch = eval('/'+SelectedText+'/g');
$("#append").append(SelectedText);
var textReplaced = alltext.replace(strSearch,'<span class="yellow">' + texto + '</span>');
$(this).html(textReplaced);
}) Aí está o código. Minha dúvida é como deixar o texto selecionado novamente, com a classe CSS "white".
Agradeço desde já.
Discussão (1)
Carregando comentários...