Jquery UI对话框条件格式

时间:2016-11-15 09:22:11

标签: javascript jquery css

我正在尝试动态更改Jquery UI对话框的格式。我有一个数组,取决于"格式"参数我传递给createDialog函数,该类应该更改("绿色"类为"良好","红色"类为"坏" ):

var array = [["bullet1","bad"],["bullet2","good"], ["bullet3","bad"],["bullet4","good"]];

$('#a-button').click(function() {
var current = 0;
//current dynamically changes in the longer version of the code
var format = array[0][1];
createDialog(array[current][0], current, format);
});

function createDialog(data, current, format) {
var $div = $('<div>');
$div.html(data).dialog({

//condition I add which breaks the page
if (array[current][1] == "bad") { format = "red";
} else {
format = "green";
}

dialogClass: format,
width:"auto",

});
}

供参考我的CSS如下:

.ui-dialog.red {
border: 3px solid red;
}

.ui-dialog.green {
border: 3px solid green;
 }

任何人都可以给我正确的语法来添加这个条件而不会破坏代码(如果删除条件,代码将有效)?

1 个答案:

答案 0 :(得分:0)

您可以使用ternary operator使dialogClass成为声明

function createDialog(data, current, format) {
var $div = $('<div>');
$div.html(data).dialog({
   dialogClass: array[current][1] == "bad"?"red":"green",
   width:"auto",
});
}