IE在if语句上抛出错误

时间:2016-11-04 23:13:47

标签: javascript internet-explorer

我不知道为什么在IE上,代码报告错误。这是代码:

if ($size === null) {
    $sizeFolder = '48x48';
} else $sizeFolder = $size+'x'+$size;

错误如下:

SCRIPTING1006: ')' expected

IE会像这样设置)

if ($size === null)) {
    $sizeFolder = '48x48';
} else $sizeFolder = $size+'x'+$size;

但这仍然无效。为什么IE报告错误?

这是完整的代码:

    $.ttMessageBox.getIcon = function ($icon,$size=null) {
  var $_icons = Array('[Information]','[Error]','[Question]','[OK]');
  if ($size === null) {
    $sizeFolder = '48x48';
  } else $sizeFolder = $size+'x'+$size; 
  $result = '';
  $Icon = $_icons.indexOf($icon);
  switch ($Icon) {
    case 0: $result += '<img src="/img/icons/'+$sizeFolder+'/dialog_information.png" />';
              break;
    case 1: $result += '<img src="/img/icons/'+$sizeFolder+'/dialog_error.png" />';
              break;
    case 2: $result += '<img src="/img/icons/'+$sizeFolder+'/dialog_question.png" />';
              break;
    case 3: $result += '<img src="/img/icons/'+$sizeFolder+'/dialog_check.png" />';
              break;
    default: $result += '';
  }
return $result;
}

2 个答案:

答案 0 :(得分:0)

错误在这一行:

   $.ttMessageBox.getIcon = function ($icon,$size=null) {

IE不支持函数参数的默认值。你不能拥有=null位。

答案 1 :(得分:0)

这是现在的工作代码:

$.ttMessageBox.getIcon = function ($icon,$size) {
  var $_icons = Array('[Information]','[Error]','[Question]','[OK]');
  if (typeof $size === "undefined") {
    $sizeFolder = '48x48';
  } else $sizeFolder = $size+'x'+$size;

感谢@Luke Woodward