javascript - Using jQuery to change the color of a part of a text in an option element - Stack Overflow

admin2025-04-20  0

Using jQuery, can we set a portion of a <option> text to certain color? For example:

<option>John, Rambo, Indiana, Jones, Morpheus, John, McClane, Forrest, Gump</option>

If I want to just change the color of "Morpheus". Is it possible?

Using jQuery, can we set a portion of a <option> text to certain color? For example:

<option>John, Rambo, Indiana, Jones, Morpheus, John, McClane, Forrest, Gump</option>

If I want to just change the color of "Morpheus". Is it possible?

Share Improve this question edited Jan 31, 2011 at 14:35 Yi Jiang 50.2k16 gold badges139 silver badges136 bronze badges asked Jan 31, 2011 at 13:36 ArulmuruganArulmurugan 1071 silver badge12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

If you meant option value of a select box, there is no easy way or even cross-browser solution. You will have to resort to some custom solution using LIs to mimic a customized select box and then you can style it with jquery.

For example, check out the one option here that you can customize (add colors, etc) however you like.

I don't see jquery helping you much here, but some plain javascript can do this:

"John,Rambo,Indiana,Jones,Morpheus,John,McClane,Forrest,Gump".replace(/Morpheus/gi, "<p style='color:blue'>Morpheus</p>")

If you'd like to change the color (using JQuery) while the application is running, you can try something like...

<SELECT>
<OPTION>What is your preferred browser?</OPTION>
<OPTION id="b1" style="color:Blue">Explorer 5.5</OPTION>
<OPTION id="r1" style="color:Red">Explorer 6.0</OPTION>
</SELECT>

Then...use JQuery to change the CSS color for the "b1" or "r1" element...

$("#b1").css("color", "#FF0000");

Try this:

$("#mySelect option:contains('Morpheus')").css("background-color","red")
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149622a287541.html

最新回复(0)