两个|中间不要有空格 function sum(x,y)
 {
    x=Number(x);
    y=Number(y);
 if(isNaN(x) || isNaN(y) )
 {
 alert("相加的两个数必须是数字,或可以转换成数字的其他类型数据");;
 }
else
   {
   return x+y;
   }
 }
 document.write(sum(1,2));
 document.write(sum(1,"a"));

解决方案 »

  1.   

    function sum(x,y) {
      try {
        x=Number(x);
        y=Number(y);
        if(isNaN(x) || isNaN(y) ) {
          throw "相加的两个数必须是数字,或可以转换成数字的其他类型数据";
        }else {
          return x+y;
        }
      }catch(e) {
        alert(e);
      }
      return '';
    }
    document.write(sum(1,2));
    document.write(sum(1,"a"));
      

  2.   

     function sum(x,y)
     {
        x=Number(x);
        y=Number(y);
     if(isNaN(x) || isNaN(y) )
     {
     alert("相加的两个数必须是数字,或可以转换成数字的其他类型数据");;
     }
    else
       {
       return x+y;
       }
     }
     document.write(sum(1,2));
     document.write(sum(1,"a"));