.p1?{?background-color:#abcdef;?color:green;?}
.p2?{?background-color:#000;?color:#fff;?}
div?{?margin:10px;?}<input?type="text"?class="js-text2"?value=""/>
<input?type="text"?class="js-text3"?value=""/>
<input?type="button"?onclick="calculate('+')"?value="加"?/>
<input?type="button"?onclick="calculate('-')"?value="減"?/>
<input?type="button"?onclick="calculate('*')"?value="乘"?/>
<input?type="button"?onclick="calculate('/')"?value="除"?/> <script>
function?calculate(a){
var?num1?=?$("js-text2").value;
var?num2?=?$('js-text3').value;
var?isnum1?=?isnumber(num1);
var?isnum2?=?isnumber(num2);
if(isnum1==2||isnum2==2){
alert("含有非法字符");
return?false;
}else{
if(?isnum1==0?&&?isnum2==0?)?{
if(?a=='+'?)?{
alert(parseInt(num1)+parseInt(num2));
}
else?if(?a=='-'?)?{
alert(parseInt(num1)-parseInt(num2));
}
else?if(?a=='*'?)?{
alert(parseInt(num1)*parseInt(num2));
}
else?if(?a=='/'?)?{
alert(parseInt(num1)/parseInt(num2));
}
}
}
}
function?$(classname){
return?document.getElementsByClassName(classname)[0];
}
/*
return?0:整數(shù);?1:小數(shù);?2:非法字符
*/
//只能整數(shù)運(yùn)算
function?isnumber(a){
if(!isNaN(a)){
var?array?=?a.split('.');
var?count?=?array.length;
if(count==1){
return?0;
}else{
if(count==2){
if(parseInt(array[1])==0){
return?0;
}else{
return?2;
}
}else{
return?2;
}
}
}else{
return?2;
}
}
</script>這是一個(gè)整數(shù)的加減乘除,我想問(wèn)的是判斷是否為整數(shù)(函數(shù) isnumber)返回的值,為什么只能是數(shù)字。我嘗試將 返回的數(shù)字改成字母就會(huì)報(bào)錯(cuò)
請(qǐng)問(wèn)這里判斷數(shù)字的返回值,為什么只能是數(shù)字?
依韻S1
2016-12-19 09:33:50