1
 <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" に数字を渡します。したがって、パラメーターを document.calculator.display.value に渡すことができるように、以下の if ステートメントで数値と演算子をチェックする方法を見つけようとしています。

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」に変更して「強調表示」した後、2 つのボタン間でテキストを交換する方法を見つけようとしています。(この質問は最初の質問とはまったく関係ありません

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="";
}
4

2 に答える 2

2

ID 属性の一部が無効です。大文字で始まる関数名は、慣例により、コンストラクター用に予約されています。

引数が数字かどうかをテストするには:

function addDigit(x) {

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

編集

いくつかの追加説明。の式/ /は、正規表現 (RegExp) オブジェクトを作成する正規表現リテラルです。オブジェクトのメソッドとオブジェクト自体は、さまざまな方法で使用できます。

パターン^\d$は、正確に 1 桁 (0 から 9) のストリングと一致します。このtestメソッドは、引数がパターンに一致するかどうかに応じてtrueorを返します。falseしたがって、式:

/^\d$/.test(x)

は、1 桁の数字に一致するパターンを持つ正規表現オブジェクトを作成し、それを使用して、指定された引数が一致するかどうかをテストし、最後にブール値trueまたはを返しfalseます。

次を使用することもできますString.prototype.match

x.match(/^\d$/)

xただし、それは文字列である必要がありますtestが、引数がまだ文字列でない場合は、引数を文字列に変換します。また、配列 (少なくとも 1 つの一致が見つかったnull場合) または (一致が見つからない場合) も返します。したがって、ブール値への型変換が必要testです。これは、最初にブール値を返すほど整然としていません。

HTH。:-)

于 2012-10-19T02:14:16.377 に答える
0

これを行うには 2 つの方法があります (他にもあると思います)。

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;
    }
}
于 2012-10-19T02:24:46.280 に答える