English 中文(简体)
jQuery UI button: How do I override the classes used for a single button?
原标题:

I am using the jQuery UI library out of the box, based on a theme.

Having links rendered as buttons is great, however I need to override some buttons with different colours.

How do I specify an specific class for a particular button to use?

最佳回答

I would say, give the particular button or buttons an id, and:

$("#buttonId").removeClass().addClass("myClass");

If you want to apply it to multiple buttons each with its own id:

$("#buttonId, #anotherButton").removeClass().addClass("myClass");
问题回答

I recommend looking at the CSS for the jQuery UI buttons and duplicating the structure of the CSS which specifies the buttons, but with your own class instead of the jQuery UI classes. Make the overrides that you need in this CSS and include it after the jQuery UI CSS. CSS uses a combination of the most specific selector and ordering to determine which values to apply. By doing this you will make sure that you have the same specificity for each of the CSS selectors used by jQuery so that your CSS takes precedence based on order.

Smashing Magazine has an article that probably has more information than you care to know about the specificity issue.

You can also:

  1. Use Developer Tools in the browser (Chrome has great ones).
  2. See what class from jQuery UI defines the button color.
  3. Override it in your CSS file with the "!important" attribute.

For example, when I needed to override jQuery UI spinner control and remove the borders, I found the class that defines the borders using Chrome Dev Tools. Then in CSS: I added something like that:

.<jquery-ui-class-that-i-found> { border: 0px !important; }

Works great!

I think the button API should include a configuration like this where you can change color etc. by passing parameters

$("button").button({background:"FFFFFF",hover:"FFFFF"}); 

this is just an idea where you can change some of its visual attributes.

I found this worked for me: $(".btnSave").removeClass("ui-state-default").addClass("SaveButtonStyling");

Basically needed to remove the ui-state-default class and then add my own for the background colour etc.

Thsi meant that the rounded corner class etc stayed put and I was able to amend the background colour etc.

If you simply wish to have some additional/different for particular buttons, simply give the buttons some classes like class="mybuttonclass otherbuttonclass" - multiple classes are allowed. Then, just add css rules for your class(es)

.mybuttonclass
{
   background-color: red;
}
.otherbuttonclass
{
   color:white;
}

thus the background is red with white text - or whatever combination you wish, which would override items in the cascade (CSS) above it. (assumption is that your .CSS file is linked in AFTER the jquery UI css file, or is in-line on the page, both of which would override the jQuery UI css.





相关问题
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: ...

热门标签