将 CSS 应用到 jQuery 对话框按钮

2024-02-29

所以我目前有一个带有两个按钮的 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)
    }
});

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

请注意,我创建的“取消”按钮是预定义类型,但“保存”是我自己定义的。不确定这是否会对这个问题产生任何影响。

任何帮助,将不胜感激。谢谢。

UPDATE:大家一致认为,来这里有两条路:

  1. 使用 Firefox 检查 HTML 插件之类firebug http://www.getfirebug.com,并注意 jQuery 的 CSS 类 应用于按钮,并采取 试图凌驾于他们之上。注:在 我的 HTML,两个按钮都使用了 完全相同的 CSS 类并且没有唯一的 ID,所以这个选项已经被淘汰了。
  2. 在打开的对话框中使用 jQuery 选择器 抓住我想要的按钮, 然后添加一个 CSS 类。

我选择了第二个选项,并使用了 jQuery find() 方法,因为我认为这比使用 :first 或 :first-child 更合适 b/c 我想要更改的按钮不一定是中列出的第一个按钮标记。使用 find,我可以指定按钮的名称,并以这种方式添加 CSS。我最终得到的代码如下:

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

我重新发帖我的答案 https://stackoverflow.com/questions/5456605/how-to-give-css-class-to-the-ok-button-of-dialog-in-jquery/5456874#5456874一个类似的问题,因为似乎没有人在这里给出它,而且它更干净整洁:

使用替代方案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)
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 CSS 应用到 jQuery 对话框按钮 的相关文章

随机推荐

Powered by Hwhale