English 中文(简体)
只改变选定选择的颜色
原标题:Change color of selected option only
  • 时间:2011-10-12 14:30:58
  •  标签:
  • jquery
  • css

我有一个挑选人坐在一个桌子里。 该表的浏览量有色,因此,通过使用<条码>背地-color:inherit;,I能够将下游的背景改变为相同的颜色。 然而,它用所有选择改变了整个箱子的颜色。

是否可能只改变选定的选择的颜色,如果可能的话,可能的话,在“Qu”的帮助下?

最佳回答

Everything is possible with jQuery :) You should try this:

$( .mySelect ).change(function () {
    $(this).find( option ).css( background-color ,  transparent );
    $(this).find( option:selected ).css( background-color ,  red );
}).trigger( change );

这里是live demo

希望帮助!

问题回答

我之所以能够这样做,是首先把选任考试课程的内容的背景颜色确定在我所希望的一切选择中。 然后,我提出了所有选择方案。 最后,我把选定的选择与选任考试课程相同的彩色办法,这样,这个选择就在下个名单中显示出同样的色。

$.each($( select ), function(i,v) {

    theElement = $(v);
    theID = theElement.attr( id );

    // Set the SELECT input element to green background with white text

    theElement.css( background-color ,  green );
    theElement.css( color ,  white );

    // Set all the options to another color (note transparant will show the green)

    $( # +theID).find( option ).css( background-color ,  white );
    $( # +theID).find( option ).css( color ,  black );

    // Finally set the selected option to the same values as the SELECT element

    $( # +theID).find( option:selected ).css( background-color ,  green );
    $( # +theID).find( option:selected ).css( color ,  white );
});

你应该能够这样做。 我可能错过(见David Thomas评论),但试图:

$("option[value= myValue ]").css( background-color ,  red );




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签