所以我现在有一个jQuery对话框,有两个按钮:保存和关闭.我使用下面的代码创建对话框:

$dialogDiv.dialog({
    autoOpen: false,
    modal: true,
    width: 600,
    resizable: false,
    buttons: {
        Cancel: function() {
                        // Cancel code here
        },
        'Save': function() {
                        // Save code here
        }
    },
    close: function() {
        // Close code here (incidentally, same as Cancel code)
    }
});

但是,使用此代码时,两个按钮的 colored颜色 相同.我希望取消按钮的 colored颜色 与保存按钮的 colored颜色 不同.有没有办法使用一些内置的jQuery选项来实现这一点?我没有从文档中得到多少帮助.

Note that the Cancel button I'm creating is a pre-defined type, but 'Save' I'm defining myself. Not sure if that will have any bearing on the issue.

任何帮助都将不胜感激.谢谢.

UPDATE: Consensus was that there were two roads to travel here:

  1. Inspect the HTML using a Firefox plugin like firebug, and note the CSS classes that jQuery is applying to the buttons, and take a stab at overriding them. Note: in my HTML, both buttons were used the exact same CSS classes and no unique IDs, so this option was out.
  2. 在打开对话框时使用jQuery Select 器

I went with the second option, and used the jQuery find() method as I think this is more appropriate than using :first or :first-child b/c the button that I wanted to change wasn't necessarily the first button listed in the markup. Using find, I can just specify the name of the button, and add CSS that way. The code I ended up with is below:

$dialogDiv.dialog({
    autoOpen: false,
    modal: true,
    width: 600,
    resizable: false,
    buttons: {
        Cancel: function() {
                        // Cancel code here
        },
        'Save': function() {
                        // Save code here
        }
    },
        open: function() {
            $('.ui-dialog-buttonpane').find('button:contains("Cancel")').addClass('cancelButtonClass');
        }
    close: function() {
        // Close code here (incidentally, same as Cancel code)
    }
});

推荐答案

我重发了my answer个类似的问题,因为似乎没有人在这里给出过,而且它更干净、更整洁:

使用备用的buttons属性语法:

$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)
    }
});

Jquery相关问答推荐

从 React 到 Node 服务器的 ajax 调用中的数据未定义

Jquery each - 停止循环并返回对象

如果您的 Select 器对象无效,为什么 jQuery 不会炸弹?

如何使用 JQuery Select 没有特定子元素的元素

未捕获的类型错误:无法读取未定义的属性toLowerCase

如何滚动到jQuery中的元素?

如何使用 jQuery 将分钟转换为小时/分钟并添加各种时间值?

有没有办法放大 D3 力布局图?

使用 D3.js(IE、safari 和 chrome)创建 SVG 后,如何保存/导出 SVG 文件?

JQuery .hasClass 用于 if 语句中的多个值

使用 MVC、C# 和 jQuery 导出为 CSV

jQuery 不会从 AJAX 查询中解析我的 JSON

好处和.在本地托管 jQuery 的trap

使用 jquery 通过文本设置下拉值

我如何知道 jQuery 是否有待处理的 Ajax 请求?

测试空 jQuery Select 结果

如何在调用 Ajax 等异步调用时让代码等待

使用javascript将HTML页面中的div下载为pdf

如何插入元素作为第一个子元素?

detach()、hide() 和 remove() 之间的区别 - jQuery