0

Is there some way by which I can change the color of all instances of string or symbol in my case * to red by html or javascript?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Amenic
  • 17
  • 2
    Yes, there are many ways to accomplish this. Have you tried anything? – Rob M. Nov 22 '13 at 18:15
  • Please post examples of the HTML you have and what you would like it to look like. We need more that just "all instances" and you need to show us what you tried already and what didn't work for you. On another note - we are an English only site (we will launch a Portuguese Stack Overflow sometime next year). – Oded Nov 22 '13 at 18:15
  • Strings don't have styles, elements do! You need to explain this a lot better. – adeneo Nov 22 '13 at 18:16

4 Answers4

2

For example:

$(document.body).html(function(i,val){
    return val.replace(/\*/g,'<span style="color:red;">*</span>');
});

Of course it may break your html syntax.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
0

Use jQuery:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

Depois adicione esse script(After add this script):

<script>
$(document).ready(function(){    
    $("[tag]:contains('*')").each(function () {
        $(this).html($(this).html().replace("*", "<span style='color:red'>*</span>"));
    });
});
</script>

Substitua [tag] pela tag na qual fica o caractere, por exemplo (Replace [tag] for the tag where * is inside, for example):

<script>
$(document).ready(function(){    
    $("p:contains('*')").each(function () {
        $(this).html($(this).html().replace("*", "<span style='color:red'>*</span>"));
    });
});
</script>
Joao Paulo
  • 1,083
  • 4
  • 17
  • 36
-2

If you want to change the * only, then there are many ways to do that!

<div>Some text * Some</div>

Write the jQuery function as:

$("button").click(function () { // on a button click
   $("div").html($("div").text().replace("*", "<span class='color'>*</span>"));
});

After adding the span to it, use CSS to change the color:

.color {
  color: someval;
}

Phew! I finally came up with a fiddle that works! :D hehehe here it is, http://jsfiddle.net/afzaal_ahmad_zeeshan/PtpGM/1/

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
-2
<div class="header">
<p>My text...</p>
<p>My text...</p>
<p>My text...</p>
<p>My text...</p>
<b>My text...</b>

<div class="body">
<p>My text...</p>

<div class="footer">
<p>My text...</p>
<p>My text...</p>
<p>My text...</p>

<!-- IMPORT JQUERY LIBRAY -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" 

type="text/javascript">

<script type="text/javascript">
$(document).ready(function(e) {


//CHANGE COLOR FOR RED OF ALL P
 $("p").css("color","red");  
//Function css aplly rules css in all document our especific node
 $(".body p").css("color","black");
$(".body p").css("background","#ccc");
//CHANGE COLOR FOR BLUE OF ALL P IN FOOTER
// .dontforindentificymyclass

    $(".footer p").css("color","blue");
 });

Sued
  • 1