如何在Javascript中的if语句中检查变量的数字和运算符

时间:2012-10-19 01:55:30

标签: javascript

 <html>
<head>
<title> Buttons</title>
<style type="text/css">

.intro{background-color:;}
.duction{background-color:blue;}
.function{background-color:grey;}
.equals{background-color:orange;}
</style>





</head>
<body>
<form name="calculator">
<input type="text"  name="display" length="50" width="100">
<div>
<input type= "button" value="7" class="intro" id="7" onclick="one(7)"></button>
<input type= "button" value="8" class="intro" id="8" onclick="one(8)"></button>
<input type= "button" value="9" class="intro" id="9" onclick="one(9)"></button>
<input type= "button" value="+" class="intro" id="+" onclick="Operate(+)"></button>
<input type= "button" value="-" class="intro" id="-" onclick="Operate(-)"></button>
<div>
<input type= "button" value="4" class="intro" id="4" onclick="one(4)"></button>
<input type= "button" value="5" class="intro" id="5" onclick="one(5)"></button>
<input type= "button" value="6" class="intro" id="6" onclick="one(6)"></button>
<input type= "button" value="x" class="intro" id="x" onclick="Operate(*)"></button>
<input type= "button" value="/" class="intro" id="/" onclick="Operate(/)"></button>
<div>
<input type= "button" value="1" class="intro" id="1" onclick="one(1)"></button>
<input type= "button" value="2" class="intro" id="2" onclick="one(2)"></button>
<input type= "button" value="3" class="intro" id="3" onclick="one(3)"></button>
<input type= "button" value="=" class="intro" id="=" onclick="Evaluate()"></button>
<div>
<input type= "button" value="0" class="intro" id="0" onclick="one(0)"></button>
<input type= "button" value="." class="intro" id="." onclick="one(.)"></button>
<input type= "button" value="c" class="intro" onclick="clearDigit()"></button>

<script type="text/javascript" src="C:\Users\LS\Desktop\QBJS\button.js">



</script>



</body>
</html>

 var timer;
 var object;
 var thing;
 var digit="";
 var result;


 function one(event)
{
  clearTimeout(timer);
   timer=setTimeout(function(){AddDigit(event);},200);

}

我的操作员按钮(+ - / x)将操作员传递给“x”,当然我的数字按钮通过 数字为“x”。所以我想弄清楚如何检查数字和运算符 下面的if语句,以便参数可以传递给document.calculator.display.value

function AddDigit(x)
{
if (x == ????)

 {document.calculator.display.value=x;}
else
   {document.calculator.display.value+=digit + x;}
}



function Evaluate()
{
  result=eval(document.calculator.display.value);
   document.calculator.display.value = result;
}

我也试图找出一种在两个按钮之间交换文本的方法,它们被“突出显示”,并且className更改为“duction”。 (这个问题与第一个完全无关。

document.ondblclick=function(button)
{
  clearTimeout(timer);

  thing=button.target;

if(thing.className=="intro")
  {thing.className="duction";}

else if(thing.className=="duction")
  {thing.className="intro";}    
}



function clearDigit()
{
  document.calculator.display.value="";
}

2 个答案:

答案 0 :(得分:2)

您的某些ID属性无效。按照惯例,以大写字母开头的函数名称保留给构造函数。

测试参数是否为数字:

function addDigit(x) {

  if (/^\d$/.test(x)) {
    // x is a single digit
  }
  ...
}

修改

进一步解释。 / /中的表达式为regular expression literal,可创建regular expression (RegExp) object。可以以各种方式使用对象的方法和对象本身。

模式^\d$匹配正好一个数字(0到9)的字符串。 test方法返回truefalse,具体取决于参数是否与模式匹配。所以表达式:

/^\d$/.test(x)

创建一个正则表达式对象,其模式匹配单个数字,然后使用它来测试提供的参数是否匹配,最后返回布尔值truefalse

您也可以使用String.prototype.match

x.match(/^\d$/)

但是这需要x作为字符串,而test会将参数转换为字符串(如果它不是一个字符串)。它还返回一个数组(如果找到至少一个匹配项)或null(如果未找到匹配项)。所以它需要将Type转换为boolean,这不像test那样整齐,它返回一个布尔值来开始。

HTH。 : - )

答案 1 :(得分:0)

这有两种方法(我相信还有更多)。

function AddDigit(x) {
    //if it parses to an integer or is a literal '.'
    if (typeof parseInt(x, 10) === 'number' || x === '.') {
        //digit or decimal
        document.calculator.display.value = x;
    } else {
        //operator: + - x /
        document.calculator.display.value += digit + x;
    }
}

function AddDigit(x) {
    //use a RegExp
    var operator = /[+-x\/]/;
    if (operator.test(x)) {
        //operator: + - x /
        document.calculator.display.value += digit + x;
    } else {
        //digit or decimal
        document.calculator.display.value = x;
    }
}