将类添加到jquery对话框按钮

nic*_*omb 21 css jquery class

我想在jquery对话框的按钮上添加css类.

这是我的代码:

$(document).ready(function(){
      $('#messageBox p').html('bla bla bla. Ok?'); 
      $('#messageBox').dialog({
        modal : true,
        buttons: {
          'Yes': function() {
            doSomething();
            $(this).dialog('close');
          }, 
          'No': function() {
            doAnotherThing();
            $(this).dialog('close');
          }
        }
      });
    });
Run Code Online (Sandbox Code Playgroud)

例如,我想在"是"按钮上添加".red"类.

我怎样才能做到这一点?

谢谢!

小智 53

buttons: [
            {
               text: "Submit",
               "class": 'submit_class_name',
               click: function() {                     
                  $(this).dialog("close"); 
               }
            },
            {
               text: "Cancel",
               click: function() { 
                  $(this).dialog("close"); 
               }
            }
          ]
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案!注意:在类属性周围使用引号,在iPad上它会抛出一个错误(可能'因为类是保留(键)字) (2认同)

nic*_*omb 8

我有解决方案,感谢Rich:

$(document).ready(function(){
      $('#messageBox p').html('bla bla bla. Ok?'); 
      $('#messageBox').dialog({
        modal : true,
        dialogClass: 'dialogButtons',
        buttons: {
          'Yes': function() {
                doSomething();
                $(this).dialog('close');
          }, 
          'No': function() {
                doAnotherThing();
                $(this).dialog('close');
          }
        }
      });
    });
$("div.dialogButtons div button:nth-child(1)").addClass("oneCssClass");
$("div.dialogButtons div button:nth-child(2)").addClass("anotherCssClass");
Run Code Online (Sandbox Code Playgroud)

解决了!


Ric*_*ich 5

对话框函数有一个dialogClass选项,可用于为对话框本身指定css类.您可以为其指定一个唯一的类名,并使用此类名来获取对话框的任何子元素的引用.然后,使用选择器按位置或其包含的文本获取对子按钮的引用(使用前者可能更有效).