javascript - JQuery if line of text starts with character, wrap whole line in span -
so have text , need give different color lines start character. have far this:
var msgtext = $(".msginfo").html(); var textlines = msgtext.split("<br>"); $(textlines).each(function(line, text) { if (text.indexof(">>") == 0) { $(".msginfo").html(function(i, html) { return html.replace(text, "<span style=\"color:#bfbfbf;\">" + text + "</span>"); }); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="msginfo">nova mensagem 2<br><br>cumpts;<br>josue<br>apt 253<br><br>>>nova mensagem 1<br>>><br>>><br>>>>>como sei para que enviei mensagem? preciso de um campo que diga para quem foi mensagem?<br>>>>><br>>>>><br>>>>>>>mensagem de teste para verificar o nome de quem enviou o email.<br>>>>>>><br>>>>><br>>><br></div>
but it's not wrapping lines. ones text other ">>" in them.
what doing wrong?
thanks
that replacing seems bit nasty , costly. how like:
var msgtext = $(".viewmessage .msg .msginfo").html(); var newtext = ''; var textlines = msgtext.split("<br>"); $(textlines).each(function(line,text){ if (text.indexof(">>") == 0){ if (text.indexof(">>") == 0){ text = "<span style=\"color:#bfbfbf;\">" + text +"</span>"; } newtext += text; }); $(".viewmessage .msg .msginfo").html(newtext);
be careful though. epascarello pointed out, jquery selector $(".viewmessage .msg .msginfo")
select multiple elements. should make sure grabs single element or "textreplace" of found elements if desired.
Comments
Post a Comment