【问题标题】:jQuery Ui Dialog Buttons, How to add class?jQuery Ui 对话框按钮,如何添加类?
【发布时间】:2011-10-05 20:20:31
【问题描述】:

我在另一个帖子上找到了这个答案..

How to add multiple buttons in Jquery UI dialog box?

使用这种语法,如何为特定按钮添加类?

 $("#mydialog").dialog({
      buttons: {
        'Confirm': function() {
           //do something
           $(this).dialog('close');
        },
        'Cancel': function() {
           $(this).dialog('close');
        }
      }
    });

【问题讨论】:

  • ...Laphroiag 威士忌和古巴雪茄。
  • @rockerest:你让我很开心。拉弗雅 10 年单一麦芽威士忌是我的首选。
  • @bpeterson76 尽可能简洁地回答了这个问题。 i18n 人知道如何使解决方案适合他们。

标签: jquery jquery-ui user-interface dialog jquery-ui-dialog


【解决方案1】:

您可以将类添加到对话框中的按钮...by

$('#mydialog').dialog({
  buttons:{
    "send":{
      text:'Send',
      'class':'save'
    },
    "cancel":{
      text:'Cancel',
      'class':'cancel'
    }
  });

我认为这对你有用。你可以找到更多答案here。

【讨论】:

  • 你应该把“className”改成“class”
  • 虽然这可行,但它会在许多 IDE(如 Netbeans)中产生错误,导致我选择了上述方法(查找按钮并添加类)。
  • 错误提示class是保留的吗?如果是这样,您可以用引号将单词括起来以避免这些错误。
  • 我认为这是正确的方法,但该类首先添加到其他 jqueryui 类中,因此 css 规则需要 !important
  • 我认为这应该被标记为答案,因为它是最好的解决方案。
【解决方案2】:

看起来没有什么好方法可以通过 API 来实现这一点,但是您可以在 create 的事件处理程序中添加类:

$("#dialog").dialog({
    buttons: {
        'Confirm': function() {
            //do something
            $(this).dialog('close');
        },
        'Cancel': function() {
            $(this).dialog('close');
        }
    },
    create:function () {
        $(this).closest(".ui-dialog")
            .find(".ui-button:first") // the first button
            .addClass("custom");
    }
});

如果你想要第二个按钮,你可以使用:

$(this).closest(".ui-dialog").find(".ui-button").eq(1).addClass("custom") // The second button

关键是$(this).closest(".ui-dialog").find(".ui-button"),它将选择对话框中的按钮。之后,您可以应用您想要的任何选择器(包括 :contains(...),如果您想根据按钮的文本而不是其顺序来选择按钮,这可能会很有用)。

这是一个例子:http://jsfiddle.net/jjdtG/

还要注意,要应用样式的 CSS 选择器必须比 jQueryUI 的内置选择器更具体,才能应用样式。

【讨论】:

  • +1 这很好用,但我使用了open 而不是create,因为我打开和关闭同一个对话框几次。
  • .dialog({dialogClass: "custom", ...}) 在文档中是这样说的。
  • @popnoodles:这是一个相当古老的答案。这可能是如今的首选方式。
  • @AndrewWhitaker 如果您只想将 .ui-priority-primary 类添加到一个按钮,仍然可能是首选。如果按钮只支持诸如 buttonClass 选项之类的东西,您可以通过定义传入。
  • 我觉得这个需要修正一下,第一个按钮eq(0)是用来关闭对话框的X按钮,所以index = 1就是第二个按钮。
【解决方案3】:

希望它会有所帮助!

$("#mydialog").dialog({
          buttons: {
            'Confirm': function() {
               //do something
               $(this).dialog('close');
            },
            "Cancel": {
                    click: function () {
                        $(this).dialog("close");
                    },
                    text: 'Cancel',
                    class: 'custom-class'
                }
          }
        });

【讨论】:

  • 因同时显示点击、文本和类的示例而被投票赞成。
  • 这很有帮助。虽然我希望 jquery 会附加该类,而不是先插入它。
【解决方案4】:

使用打开事件处理程序:

open: function(event) {
     $('.ui-dialog-buttonpane').find('button:contains("Cancel")').addClass('cancelButton');
 }

干净,简单,小菜一碟!

【讨论】:

  • 别忘了 i18n!如果网站支持多种语言,此方法将不起作用
  • 如果您将按钮的字符串替换为语言的值,它可以尊重 i18n。如果需要,您还可以更改选择器。 i18n 不是 OP 问题的一部分……对于大多数开发人员来说,这在很大程度上是一个边缘案例。
  • @bpeterson76:也许是 Oracle 的大多数开发人员,但在美国以外,i18n 是一个主要问题...... ' 日期格式。
【解决方案5】:

感谢 LintonB,您可以添加所有附加到按钮的属性,例如样式、id 等...

dialog_options.buttons = {
  'Modify': {
    click: function() {

      $(this).dialog('close');

      if (inputs.phone !== '') {
        inputs.phone.focus();
        inputs.phone[0].value = "";
      }
    },
    class: 'btn btn-labeled btn-warning',
    style: 'margin-right: 30px;',
    id: 'modificationId'
  },
  'Keep': {
    click: function() {
      $(this).dialog('close');

      _this.validatePhone(i);

    },
    class: 'btn btn-labeled btn-warning',
    id: 'conserverId'
  }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2016-05-31
    • 2010-12-01
    • 1970-01-01
    • 2011-12-27
    相关资源
    最近更新 更多