Coloring text dynamically
I have a webiste, that takes an in开发者_C百科put string, and marks part of it with a color, according to a dropDownList.
this is done with replacing a regex and a generated tag with style.
Is it better practice to surround it with a with a specific class and set the style of that class in the css (again it has to change according to the dropdown)
meaning every time the "mark" button is matched, the script will change a rule in the CSS.
is it possible to do so, and how exactly. I couldn't find any way to access a css rule by its name.
<div id="dv">sample text</div>
<input type="button" onclick="changeColor('#959562');">
<script language="javascript">
function changeColor(color)
{
jQuery('#dv').css('color',color);
}
</script>
use this type of code you can change color dynamically by using jQuery
If the number of colors is not known ahead of time you can simply do
<span style="background-color:#424242">text </span>
in your regular expression. Replace the number with color of your choice.
You shouldn't change the CSS rule, have several CSS rules that correspond to the different colours.
The event handler for the drop down should then update the class attribute of the input string.
精彩评论