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

标签 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);

}

我的运算符(operator)按钮(+-/x)将运算符(operator)传递给“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="";
}

最佳答案

您的某些 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)

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

你也可以使用 String.prototype.match :

x.match(/^\d$/)

但这要求 x 是一个字符串,而 test 会将参数转换为字符串(如果它还不是字符串)。它还返回一个数组(如果至少找到一个匹配项)或 null(如果未找到匹配项)。所以它需要将类型转换为 bool 值,这不像 test 那样整洁,它返回一个 bool 值作为开头。

HTH。 :-)

关于javascript - 如何在 Javascript 的 if 语句中检查变量中的数字和运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12966310/

相关文章:

javascript - 有什么方法可以使用 javascript 来检测 javascript 事件处理程序触发吗?

javascript - Phonegap 仅在第一次时显示登录页面

javascript - Django 尝试将我的数组保存到模型中

javascript - 在不涉及输入字段的情况下,在按下按钮时显示 jQueryUI 日期选择器

javascript - Angular 旋转器 : functions execution order

javascript - 在 div 列表中的随机位置插入一个 div

javascript - 从 Node 中的其他文件调用类实例的函数

javascript - Codecademy 上的 JavaScript 控制流编程

javascript - 使用没有 ID 的 Javascript 选择单选按钮

javascript - 如何使用历史推送状态