未捕获的ReferenceError:HTMLButtonElement.onclick中未定义计算器

时间:2017-03-04 12:54:32

标签: javascript

开发人员工具/疑难解答告诉我,HTMLButtonElement.onclick中未定义计算器。一切正常,除了我没有得到答案。有谁看到我做错了什么?我举了一个例子,一个例子有效。但不是我的代码。当我点击按钮时,我得到了四个按钮和两个文本字段,但没有答案,文本字段中有数字。

<!DOCTYPE html>
       <html>
         <head>
           <title>oppg.3</title>
         </head>
       <body>

         <input id="v1" type="text" style="width:5em;"/>
         <button onclick="calculator.sum();"> + </button>
         <button onclick="calculator.multiply();"> * </button>
         <button onclick="calculator.div();"> / </button>
         <button onclick="calculator.mod();"> % </button>
         <input id="v2" type="text" style="width:5em;"/>
         <span id="result"> </span>


       <script type="text/javascript">
    var Calculator = function(inp1,inp2,reslt) {
        this.valinp1 = inp1;
        this.valinp2 = inp2;
        this.result = reslt;
        if (!Calculator.prototype.sum) {
          Calculator.prototype.sum = function() {
            var value1 = Number(this.valinp1.value),
                value2 = Number(this.valinp2.value);

                this.result.innerHTML =
                 (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1)))
                 : (value1 + value2);
            };
            Calculator.prototype.multiply = function() {
              var value1 = Number(this.valinp1.value);
              var value2 = Number(this.valinp2.value);
              this.result.innerHTML =
                (!/[0-9]/.test(string(value1)) || !/[0-9]/.test(string(value1)))
                :(value1*value2);

            };
            Calculator.prototype.div = function() {
               var value = Number(this.valinp1.value);
               var value2 = Number(this.valinp2.value);
               var restmp = value1/value2;
               this.result.innerHTML =
                 (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1)) || value2 === 0)
                 : restmp%1 ? restmp.toFixed(3) : restmp;
            };
            Calculator.prototype.mod = function() {
              var value1 = Number(this.valinp1.value);
              var value2 = Number(this.valinp2.value);
              var restmp = value1%value2;
              this.result.innerHTML =
              (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1)) || value2 === 0)
              : value1%value2;

            };
          }
      };
      calculator =
         new Calculator (
             document.getElementById('v1'),
             document.getElementById('v2'),
             document.getElementById('result')
        );
        </script>


      </body>
    </html>

1 个答案:

答案 0 :(得分:0)

这是一个工作代码段,您的验证代码中存在一些拼写错误和语法错误。

var Calculator = function(inp1,inp2,reslt) {
        this.valinp1 = inp1;
        this.valinp2 = inp2;
        this.result = reslt;
        if (!Calculator.prototype.sum) {
          Calculator.prototype.sum = function() {
            var value1 = Number(this.valinp1.value),
                value2 = Number(this.valinp2.value);

                this.result.innerHTML =
                 (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1))) ? 'error'
                 : (value1 + value2);
            };
            Calculator.prototype.multiply = function() {
              var value1 = Number(this.valinp1.value);
              var value2 = Number(this.valinp2.value);
              this.result.innerHTML =
                (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1))) ? 'error'
                :(value1*value2);

            };
            Calculator.prototype.div = function() {
               var value1 = Number(this.valinp1.value);
               var value2 = Number(this.valinp2.value);
               var restmp = value1/value2;
               this.result.innerHTML =
                 (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1)) || value2 === 0)
                  ? restmp.toFixed(3) : restmp;
            };
            Calculator.prototype.mod = function() {
              var value1 = Number(this.valinp1.value);
              var value2 = Number(this.valinp2.value);
              var restmp = value1%value2;
              this.result.innerHTML =
              (!/[0-9]/.test(String(value1)) || !/[0-9]/.test(String(value1)) || value2 === 0) ? 'error'
              : value1%value2;

            };
          }
      };
      calculator =
         new Calculator (
             document.getElementById('v1'),
             document.getElementById('v2'),
             document.getElementById('result')
        );
<input id="v1" type="text" style="width:5em;"/>
         <button onclick="calculator.sum();"> + </button>
         <button onclick="calculator.multiply();"> * </button>
         <button onclick="calculator.div();"> / </button>
         <button onclick="calculator.mod();"> % </button>
         <input id="v2" type="text" style="width:5em;"/>
         <span id="result"> </span>