CreateTime--2016年12月16日17:36:20
Author:Marydon三元运算符 条件 ? 成立执行 : 不成立执行/** * 设置弹出对话框的宽和高 */function fixDialogSize (height,width) { /*设置minHeight=200px,minWidth=400px*/ /*使用三元运算符实现*/ height = height ? (height < 200 ? 200 :height) : 200; width = width ? (width < 400 ? 400 : width) : 400; /*方法二*/ if (height) { if (height < 200) { height = 200; } else { height = height; } } else { height = 200; } if (width) { if (width < 400) { width = 400; } else { width = height; } } else { width = 400; }}
需要注意的是:
1.这里的height和width 虽然没有用var进行声明,但是它并没有变成全局变量,因为这里的width和height代表的是函数的形参,只是对形参赋值并没有声明从属于函数或全局的变量;(详情见例子:demo-三元运算符) 2.使用javascript设置宽和高时,数值后面一定要加上 "px",否则不会生效。