Apply CSS to jQuery Dialog Buttons

前端 未结 10 2032
攒了一身酷
攒了一身酷 2020-12-01 01:02

So I currently have a jQuery dialog with two buttons: Save and Close. I create the dialog using the code below:

$dialogDiv.dialog({
    autoOpen: false,
             


        
相关标签:
10条回答
  • 2020-12-01 01:47

    Select the div which has role dialog then get the appropriate buttons in it and set the CSS.

    $("div[role=dialog] button:contains('Save')").css("color", "green");
    $("div[role=dialog] button:contains('Cancel')").css("color", "red"); 
    
    0 讨论(0)
  • 2020-12-01 01:50

    You should change the word "className" for "class"

    buttons: [ 
        { 
            text: "Cancel",
            class: 'ui-state-default2', 
            click: function() { 
                $(this).dialog("close"); 
            } 
        }
    ],
    
    0 讨论(0)
  • 2020-12-01 01:52

    I’m reposting my answer to a similar question because no-one seems to have given it here and it’s much cleaner and neater:

    Use the alternative buttons property syntax:

    $dialogDiv.dialog({
        autoOpen: false,
        modal: true,
        width: 600,
        resizable: false,
        buttons: [
            {
                text: "Cancel",
                "class": 'cancelButtonClass',
                click: function() {
                    // Cancel code here
                }
            },
            {
                text: "Save",
                "class": 'saveButtonClass',
                click: function() {
                    // Save code here
                }
            }
        ],
        close: function() {
            // Close code here (incidentally, same as Cancel code)
        }
    });
    
    0 讨论(0)
  • 2020-12-01 01:57

    I think there are two ways you can handle that:

    1. Check using something like firebug if there is a difference (in class, id, etc.) between the two buttons and use that to address the specific button
    2. Use something like :first-child to select for example the first button and style that one differently

    When I look at the source with firebug for one of my dialogs, it turns up something like:

    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
        <button class="ui-state-default ui-corner-all ui-state-focus" type="button">Send</button>
        <button class="ui-state-default ui-corner-all" type="button">Cancel</button>
    </div>
    

    So I could for example address the Send button by adding some styles to .ui-state-focus (with perhaps some additional selectors to make sure I override jquery's styles).

    By the way, I´d go for the second option in this case to avoid problems when the focus changes...

    0 讨论(0)
提交回复
热议问题