jQuery UI:如何向.dialog()发送一组常用选项?

时间:2011-06-03 15:41:16

标签: javascript jquery jquery-ui jquery-ui-dialog

是否可以发送一组常用选项:

var commonVars = {
    autoOpen:       false,
    draggable:      false,
    resizable:      false,
    show:           'fade',
    hide:           'fade'
};

到对话框:

$('#dialog_1').dialog({
    //Common vars go here somehow
    width:          275,
    height:         170,
    dialogClass:    "class1 class2"
});
$('#dialog_2').dialog({
    //Common vars go here somehow
    width:          600,
    height:         350,
    dialogClass:    "class3 class4"
});     

3 个答案:

答案 0 :(得分:5)

$.extend()

示例:

var object1 = {
    //Common vars go here somehow
    width:          275,
    height:         170,
    dialogClass:    "class1 class2"
};

var object2 = {
//Common vars go here somehow
width:          600,
height:         350,
dialogClass:    "class3 class4"
}
var commonVars = {
    autoOpen:       false,
    draggable:      false,
    resizable:      false,
    show:           'fade',
    hide:           'fade'
};

$.extend(object1, commonVars);
$.extend(object2, commonVars);

$('#dialog_1').dialog(object1);
$('#dialog_2').dialog(object2);

答案 1 :(得分:1)

想出来,有点意外。所以对于想知道同样事情的人,你可以在每个对话框的选项括号之前放置commonVars变量:

var commonVars = {
    autoOpen:   false,
    draggable:  false,
    resizable:  false,
    show:       'fade',
    hide:       'fade'
};

$('#dialog_1').dialog(commonVars,{
    width:          275,
    height:         170,
    dialogClass:    "class1 class2"
}); 

答案 2 :(得分:0)

你为什么不这样做$('#dialog_1').dialog(commonVars)