2012-04-18 33 views
1

的按鈕添加一個類我有一個jQuery的對話框:我的jQuery的對話框

// Configure buttons 
    var dialogButtons = {}; 

    dialogButtons[buttonConfirm] = function() { 
     $.ajax({ 
      type: "Post", 
      url: href, 
      cache: false, 
      success: function (data) { var func = success; window[func](data); } 
     }); 
     $(this).dialog("close"); 
    }; 

    dialogButtons[buttonCancel] = function() { 
     $(this).dialog("close"); 
    }; 

    // Configure dialog 
    $dialog.dialog(
     { 
      modal: true, 
      closeOnEscape: true, 
      resizable: false, 
      buttons: dialogButtons 
     }); 

    // Opening dialog 
    $dialog.dialog('open'); 

我的問題:我想設置一個特定的類「BTN」我的按鈕。我怎樣才能做到這一點?

感謝

+1

布拉德的小提琴的叉爲什麼你要添加的按鈕一類?使用他們的類('ui-button')你應該避免輪子重新創建... – gdoron 2012-04-18 12:46:01

+0

我有一個特定的CSS文件,標準look'n感覺我的所有表,按鈕,...當添加類btn或主要或。 ..在我的按鈕(任何地方)我可以改變主要和其他一些很酷的東西的顏色。 – Bronzato 2012-04-18 12:49:22

回答

1

@Colin有一個答案,但我想我會更具體的問題的對話。 jQuery-UI有一個widget方法,它返回由對話框本身組成的元素。與定位ui-button類耦合這一點,你可以讓你在找什麼:

$dialog.dialog('widget') // Grab widget (UI element) 
    .find('.ui-button') // Locate buttons within 
    .addClass('btn');  // hadd btn class to them 

編輯:另外,這裏有一個例子:http://jsfiddle.net/8WckB/

+0

它像一個魅力。清潔。非常感謝你。 – Bronzato 2012-04-18 12:53:24

1
// Configure dialog 
$dialog.dialog({ 
    modal: true, 
    closeOnEscape: true, 
    resizable: false, 
    buttons: dialogButtons, 
    open: function(e, ui) { 
     $(this).parents(".ui-dialog").find(".ui-dialog-buttonset"); // here is your 
                    // button container 
     $(this).parents(".ui-dialog").find(".ui-dialog-buttonset .ui-button").addClass("btn"); // whereas this would select 
                           // all buttons in button container 
     // you could even use the widget method 
     // $(this).dialog("widget").find(".ui-dialog-buttonset .ui-button") 
    } 
}); 
0

如果您在源看看,在jquery.ui.dialog.js_createButtons方法中,您會看到按鈕文本編制索引的散列如果不是函數,則會被視爲屬性集合。所以,你可以做到以下幾點:

var $dlg = $('#dlg').dialog({ 
    buttons: { 
     'firstButton': {    
      'click': function() { 
       $dlg.dialog('close'); 
      }, 
      'text' : 'OK',   
      'class': 'myclass' 
     }, 
     'Cancel': function(){ 
      $dlg.dialog('close'); 
     } 
    } 
}); 

這裏的演示代碼http://jsfiddle.net/uWnpy/

+0

謝謝。很高興知道。 – Bronzato 2012-04-18 16:24:22