Einfaches Beispiel für Addition, Subtraktion, Multiplikation und Abteilung in JavaScript
<html> <head> <meta http-äquiv = "content-type" content = "text/html; charSet = utf-8"/> </head> <script Sprache = "JavaScript" Typ = "text/JavaScript"> // Divide-Funktion Accdiv (arg1, arg2) {var t1 = 0, t2 = 0, T2 = 0, T2 = 0, R1, R2, R2, n; try {t1 = arg1.toString (). Split (".") [1] .Length; } catch (e) {t1 = 0;} try {t2 = arg2.toString (). split (".") [1] .Length; } catch (e) {t2 = 0;} mit (math) {r1 = nummer (arg1.toString (). ersetzen (".", ")); n = math.max (t1, t2); return (r1/r2)*pow (10, t2-t1); }} // Multiple Funktion accmul (arg1, arg2) {var t1 = 0, t2 = 0, r1, r2; try {t1 = arg1.toString (). Split (".") [1] .Length; } catch (e) {t1 = 0;} try {t2 = arg2.toString (). split (".") [1] .Length; } catch (e) {t2 = 0;} mit (math) {r1 = nummer (arg1.toString (). ersetzen (".", ")); return (r1*r2)/pow (10, t2+t1); }} // Funktion accadd (arg1, arg2) {var t1 = 0, t2 = 0, m; try {t1 = arg1.toString (). Split (".") [1] .Length; } catch (e) {t1 = 0;} try {t2 = arg2.toString (). split (".") [1] .Length; } catch (e) {t2 = 0;} mit (math) {m = math.pow (10, math.max (t1, t2)); return (arg1 * m + arg2 * m) / m; }} // Subtraktionsfunktion ACCSUBTR (arg1, arg2) {var t1 = 0, t2 = 0, m, n; try {t1 = arg1.toString (). Split (".") [1] .Length; } catch (e) {t1 = 0;} try {t2 = arg2.toString (). split (".") [1] .Length; } catch (e) {t2 = 0;} mit (math) {// Dynamische Steuerungspräziationslänge n = math.max (t1, t2); m = math.pow (10, n); // return (arg1 * m - arg2 * m) /m; return ((arg1 * m - arg2 * m) / m) .Tofixed (n); }} // Fügen Sie dem String -Typ eine DIV -Methode hinzu, die bequemer zu rufen ist. String.prototype.div = function (arg) {return accdiv (this, arg);} // Fügen Sie dem String -Typ eine MUL -Methode hinzu, sodass es bequemer ist, sie anzurufen. String.Prototype.mul = function (arg) {return accmul (arg, this);} // Fügen Sie dem String -Typ eine Methode hinzu, wodurch es bequemer ist, sie anzurufen. String.prototype.add = function (arg) {return accadd (arg, this);} // Fügen Sie dem String -Typ eine Subtr -Methode hinzu, sodass es bequemer zu rufen. String.Prototype.subtr = function (arg) {return acsubtr (this, arg);} Funktion cal () {var arg1 = document.form1.textbox1.Value; var arg2 = document.form1.textbox2.Value; //document.form1.textbox5.Value = Accdiv (arg1, arg2); //document.form1.textbox6.Value = accmul (arg1, arg2); //document.form1.textbox7.Value = accadd (arg1, arg2); //document.form1.textbox8.Value = ACCSUBTR (arg1, arg2); document.form1.textbox5.Value = arg1.div (arg2); document.form1.textbox6.Value = arg1.mul (arg2); document.form1.textbox7.Value = arg1.add (arg2); document.form1.TextBox8.Value = arg1.subtr (arg2);} </script> <body> <form id = "Form1" name = "Form1" methode = "post" runat = "server"> <div style = "borde 1px #000000; width: 600px; value = "0" name = "textbox1" /> < /div> <div style = "float: links; width: 30%;"> <input id = "textbox2" value = "0" type = "text" name = "textbox2" /> < /div> <div> <div style = "float: links; width: 30%; <Div> accmul: <input id = "textBox6" type = "text" name = "textBox6"/> </div> <div> accadd: <input id = "textbox7" type = "text" name = "textbox7"/> </div> <div> AccsubTr: <input id = "textbox" type "type" type = "text" type "text" = "text" = "text" = "text" = "text" = "text" = "text" = "text" = "text" = "text" = "text Breite: 10%; "> <Eingabe type =" button "name =" aa "value =" cal "onclick =" cal (); " /> </div> </div> </form> </body> </html>Das obige einfache Beispiel für Addition, Subtraktion, Multiplikation und Aufteilung von JavaScript ist der gesamte Inhalt, den ich mit Ihnen teile. Ich hoffe, Sie können Ihnen eine Referenz geben und ich hoffe, Sie können wulin.com mehr unterstützen.